views:

27

answers:

1

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?

+1  A: 

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.

SLaks
Do you know any examples?
Al Bundy
Examples of what?
SLaks
Of how "application can make its own SynchronizationContext", i.e. application wihtout UI loop, like Windows service.
Al Bundy
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