高响应性多线程WPF应用程序 [英] Highly Responsive Multi Threaded WPF Application
问题描述
我正在研究WPF应用程序,以通过串行端口与16个设备进行通信.通信过程包括读写命令.设备的配置大约需要2分钟.我的UI在配置过程中应具有响应性,应在执行读取命令后显示读取,并在执行写入命令后显示命令状态.我使用backgroundworker处理一台设备.一切正常.现在,我需要同时运行所有16个设备.我该怎么做?由于只有一个UI线程可用,因此UI会挂起2-3个设备.请为此建议最佳的方法.
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.
推荐答案
您必须为每个设备配备背景工, 您可以通过以下方式报告每个设备的进度:
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;
}
您还可以使用以下任何一种线程与UI线程进行交互:
you can also interact with the UI thread from any thread by using:
Application.Current.Dispatcher.BeginInvoke
( DispatcherPriority.Normal,new Action(() => { ... }));
这篇关于高响应性多线程WPF应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!