问题
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