Dispatcher is not coming on C# Windows Forms Application

喜欢而已 提交于 2019-12-07 01:31:08

问题


I have a C# Windows Forms Application with a progress bar. I want to progress that dynamically based on some method status. I have that method in a separate class, and am creating a new thread for that method, with a delegate to pass the status of the method to the parent thread.

The same situation I am able solve in a WPF application using progressbar.Dispatcher but in a Windows Forms application there is no Dispatcher, even if I use the System.Threading namespace.

progressbar.Dispatcher seems to be available only in a WPF Application.


回答1:


In winforms you can use the ProgressBar.Invoke or ProgressBar.BeginInvoke to update the control from another thread.




回答2:


In Windows Form application BackgroundWorker should fit perfectly for your task. In particular, it has ReportProgress method to send progress depending on your calculations and ProgressChanged to track changes from UI thread. See MSDN article for full details




回答3:


In WinForms the Invoke/BeginInvoke methods are directly on the control objects as you can see from the docs of System.Windows.Forms.Control. So you'd have progressBar.BeginInvoke(...) for example.



来源:https://stackoverflow.com/questions/13524327/dispatcher-is-not-coming-on-c-sharp-windows-forms-application

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