Winforms - Invoking a method in another thread

五迷三道 提交于 2021-01-27 19:24:22

问题


What is the best practice to invoke a method in a different thread from a winform button so the ui doesn't freeze or creates a delay?


回答1:


In a first step start with

  • BackgroundWorker

If this doesn't meet your requirements or you need more advanced stuff you should take a look into one of these:

  • Task Parallel Library
  • ReactiveExtensions
  • async / await



回答2:


Invoke((MethodInvoker) delegate {
    DoSomething();
});



回答3:


You should call Control.Invoke or BeginInvoke, see in-depth reference here.




回答4:


You can do like this

 Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
 {         
      //DO SOMETHING         
 }


来源:https://stackoverflow.com/questions/14338504/winforms-invoking-a-method-in-another-thread

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