Overview
Understanding threading is crucial for developing robust and reliable PACZ component plug-ins. The SDK provides strong threading guarantees that simplify development while ensuring thread safety. This guide explains how threading works in the plug-in architecture and how to safely use advanced threading patterns.Thread safety guarantees
The PACZ component plug-in SDK provides automatic thread safety for both the Runner and Builder UI portions of your plug-in.Runner thread
The Runner executes on a single, unique, dedicated thread:- All calls to the Runner are serialized and marshaled to this thread
- You never need to worry about concurrent access to Runner state
- Multiple run requests are queued and executed one at a time
- This provides a safe environment where you can maintain state without locks
- Write Runner code as if it’s single-threaded
- Use async/await code when available for performance
- No need for locks, mutexes, or other synchronization primitives for Runner state
- State variables in the Runner class are safe to use without protection
Builder UI thread
The Builder UI also runs on a single, unique, dedicated thread:- All UI operations and callbacks are marshaled to this thread
- The UI thread is different from the Runner thread
- This follows standard UI threading patterns (similar to WPF/WinForms)
- Write UI code as if it’s single-threaded
- Never directly call blocking methods. Use async/await alternatives,
Thread.Run(), orIHarnessBuilderUIHost.CallRunnerAsync<T>() - UI elements MUST only be accessed from the UI thread
- To call the Runner from the UI, use
IHarnessBuilderUIHost.CallRunnerAsync<T>()
Communicating between UI and runner
Since the Builder UI and Runner operate on different threads, the SDK provides a safe communication mechanism.Calling the runner from the builder UI
Always useIHarnessBuilderUIHost.CallRunnerAsync<T>() to invoke Runner methods from the Builder UI:
Do not use UI from the runner
The runner should never create, modify, or display any UI.Task asynchronous programming (async/await)
The SDK fully supports the .NET Task Asynchronous Programming (TAP) pattern usingasync and await.
Synchronization context
The SDK installs aSynchronizationContext for both the Runner and Builder UI threads:
- Continuations after
awaitautomatically return to the same thread - This makes async/await safe and convenient to use
- You can write natural asynchronous code without worrying about thread affinity
Example: Asynchronous operations in the runner
Running work on the thread pool
If you want to explicitly run work on the .NET thread pool instead of the Runner or UI thread, and you have CPU intensive or blocking calls, use a long-running operation:- Do not access Runner or UI state from background or thread pool threads. Return values instead
- Do not use
Task.Run()as it rarely is the right thing to do and often causes subtle performance issues or hangs. - You are responsible for using synchronization primitives such as locks to protect any data you wish to share between threads.
Session management
Managing connections to external tools (Excel, CATIA, databases, etc.) requires careful consideration of threading and resource lifecycle.Expensive resource initialization
If your plug-in uses an expensive-to-create resource:- Lazy initialization: Create the resource on first use in
RunAsync()rather than inConstructAsync() - Caching: Store the resource as a field in the Runner class
- Cleanup: Implement
IDisposableif reliable cleanup is required - UI access: Store the external resource in the runner. Use
CallRunnerAsync()from the UI to access it in a thread-safe way.
Sharing sessions between builder UI and runner
The Builder UI and Runner run on different threads and may need to share access to an external tool. Only access the tool from the Runner, call it viaCallRunnerAsync() from UI
Debugging threading issues
Common symptoms
- Deadlocks (plug-in hangs)
- Race conditions (intermittent incorrect results)
- Cross-thread exceptions (especially with UI controls)
Best practices summary
- Trust the SDK: Let the SDK handle thread marshaling for Runner and Builder UI
- Use async/await: Leverage TAP for clean, efficient asynchronous code
- Minimize shared state: Avoid mutable state shared between threads
- Document thread requirements: Clearly comment which methods expect which thread
- Keep it simple: Only use manual threading when necessary
Additional resources
- .NET Task Asynchronous Programming (TAP)
- SynchronizationContext in .NET
- Thread-Safe Collections in .NET
Questions?
If you have threading-related questions not covered here, please:- Check the FAQ for common issues
- Review example plug-ins in the repository
- Contact Ansys support for assistance