2

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?

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
Kimi
  • 13,621
  • 9
  • 55
  • 84

1 Answers1

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.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 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