Highly Responsive Multi Threaded WPF Application

你说的曾经没有我的故事 提交于 2019-12-12 06:09:10

问题


I Am Working on WPF Application to communicate with 16 Devices through Serial Port. Communication Process includes read and write Commands. Configuration of device takes around 2 minutes. My UI should be responsive during configuration process, that it should show reading after read commands execution and show command status after write commands execution. I used backgroundworker for handling one device. Everything works fine. Now i need to run all 16 devices simultaneously. How do i do it? Since Only one UI thread is available, UI hangs for 2-3 devices. Please suggest best possible way for this.


回答1:


you have to have background Worker for each device, you can report the progress for each device by:

backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged +=backgroundWorker1_ProgressChanged;

   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
     {
       ....
       backgroundWorker1.ReportProgress(i);
       ....
     }

  private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
       progressBar1.Value = e.ProgressPercentage;
    }

you can also interact with the UI thread from any thread by using:

Application.Current.Dispatcher.BeginInvoke
( DispatcherPriority.Normal,new Action(() => { ... }));


来源:https://stackoverflow.com/questions/15879375/highly-responsive-multi-threaded-wpf-application

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