BackgroundWorker still needs to call Invoke?

ε祈祈猫儿з 提交于 2019-12-01 11:34:20

The RunWorkerCompleted callback is marshalled onto the UI thread; DoWork is not.

You should use the ReportProgress method to update the UI during DoWork processing.

See: How to: Run an Operation in the Background

You are mistaken, you can't call the UI thread directly from the handler for the DoWork method, as this is on the background thread.

If you want to update the UI, you should call the ReportProgress method and then update the UI from the event handler for the ProgressChanged event.

While you can call the Invoke method in the background thread, doing so defeats the purpose of using the BackgroundWorker class. The ProgressChanged event is thrown on the UI thread and is the mechanism you should use to update the UI components when something changes on the background thread.

Keep in mind that if you call RunWorkerAsync() from non UI thread you will need to call Invoke from the ProgressChanged and RunWorkerCompleted event handlers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!