Can somebody tell me when to use a Dispatcher
and when to use the SynchronizationContext
class?
For a while now I have been using the Dispatcher
to queue up tasks from a background thread, then I discovered the SynchronizationContext
.
Can somebody tell me when to use a Dispatcher
and when to use the SynchronizationContext
class?
For a while now I have been using the Dispatcher
to queue up tasks from a background thread, then I discovered the SynchronizationContext
.
私の知る限り、WPFを使用する場合、オブジェクトは実際には Dispatcher オブジェクトの単なるラッパーであるSynchronizationContext.Current
タイプであり、 andメソッドはandにデリゲートするだけです。DispatcherSynchronizationContext
Post
Send
Dispatcher.BeginInvoke
Dispatcher.Invoke
だから、使うことにしたとしても、SynchronizationContext
裏でディスパッチャを呼んでしまうと思います。
また、UI を呼び出す必要があるすべてのスレッドに現在のコンテキストへの参照を渡す必要があるため、SynchronizationContext を使用するのは少し面倒だと思います。
コードが WPF と密結合している場合は、Dispatcher を使用します。
「Context」スレッドで何かをキューに入れる必要がある場合は、AsyncOperationManager を使用します。これは、Windows フォーム、ASP .NET、および WCF アプリケーションでも機能します。
SynchronizationContext を自分で使用することは避けてください。AsyncOperationManager は、このメカニズムを内部で使用します。