Why doesn't a BackgroundWorker need Invoke in the ProgressChanged event handler?

后端 未结 1 974
死守一世寂寞
死守一世寂寞 2021-01-14 03:00

Since the ProgressChanged event handler is raised from somewhere within the DoWork event handlers, shouldn\'t they be called on the asynchronous op

相关标签:
1条回答
  • 2021-01-14 03:13

    When you call RunWorkerAsync, the BackgroundWorker internally creates a new AsyncOperation associated with the current synchronization context, as retrieved through the AsyncOperationManager.SynchronizationContext static property.

    This synchronization context would be an instance of a class deriving from SynchronizationContext. The specific type depends on the synchronization model provider your application uses. If you’re running Windows Forms, it would be WindowsFormsSynchronizationContext; on WPF; it would be DispatcherSynchronizationContext.

    When you subsequently call ReportProgress on the background thread, the BackgroundWorker would internally call Post on the aforementioned SynchronizationContext instance, thereby dispatching the operation to the associated thread asynchronously.

    In Windows Forms, this is implemented as a Control.BeginInvoke call; on WPF, it becomes a Dispatcher.BeginInvoke call.

    0 讨论(0)
提交回复
热议问题