I'm writing a component that would be used in WinForms or WPF applications also could be used in Windows services or Unit tests. Is there a way to use SynchronizationContext component in non UI apps?
Asked
Active
Viewed 443 times
1 Answers
2
Any (host) application can make its own SynchronizationContext
class and call SynchronizationContext.SetSynchronizationContext
.
Alternatively, you could make a no-op SynchronizationContext
in your library, then use it whenever SynchronizationContext.Current
is null
.
The question talks about writing a custom SynchronizationContext.
-
Of how "application can make its own SynchronizationContext", i.e. application wihtout UI loop, like Windows service. – Kimi Jul 30 '10 at 08:57
-
If you need the callbacks to execute on a single thread, you'll need to make a thread-safe queue of pending callbacks and an infinite loop on the callback thread that executes the first callback on the queue. – SLaks Jul 30 '10 at 09:21