我应用了一个计时器,每隔20ms从A / C获取新数据.经过一些测试后,看起来添加一个单独的线程来处理每个图表,这个图表会在给定时间内休眠,然后更新图表效率不高(至少按照我这样做的方式).
所以问题是:如何有效地处理多个图表.
下面我介绍代码中最重要的部分.
System.Timers.Timer tim = new System.Timers.Timer(20); System.Threading.Thread[] t; int HighChan = 15; //button which runs the preview,executed once private void previewB_Click(object sender,EventArgs e) { t = new System.Threading.Thread[HighChan + 1]; for (int i = 0; i <HighChan+1; i++) { charts[i].Series.SuspendUpdates(); //run a separate thread for each chart t[i] = new System.Threading.Thread(new ParameterizedThreadStart(updatePlot)); t[i].Start(i); } //run timer to get new data with tim.interval tim.Stop(); tim.Elapsed += new ElapsedEventHandler(this.OnTimedEvent); tim.Start(); } ushort[] ADData_prev; //get new data from A/C every tim.interval private void OnTimedEvent(object sender,EventArgs e) { ADData_prev = getPrev(); //gets new data,array wit 16 fields // I also tried to suspend and resume threads t from this place but unsuccessfully } //update the chart with new data void updatePlot(object chart_info) { int i = (int)chart_info; while(true) { //wait for new data to read Thread.CurrentThread.Join(20); charts[i].Invoke(new Action(delegate() { charts[i].ResetAutovalues(); })); // I skipped some conditions to make code clearer //remove old point and add new one charts[i].Invoke(new Action(delegate() { charts[i].Series[0].Points.RemoveAt(0); charts[i].Series[0].Points.AddY(ADData_prev[i]); })); charts[i].Invoke(new Action(delegate() { charts[i].Series.ResumeUpdates(); charts[i].Series.Invalidate(); charts[i].Series.SuspendUpdates(); })); } }
更新:
1.我已将updatePlot()的功能移到了计时器的onTimedEvent,所以现在它看起来像这样:
private void OnTimedEvent(object sender,array wit 16 fields charts[0].Invoke(new Action(delegate() { for (int i = 0; i < HighChan + 1; i++) { //charts[i] stuff here } } }
2.我决定更改onTimedEvent中的bool变量,它允许绘制一次图表,每个计时器的滴答在updatePlot()中的while(true)循环中:
private void previewB_Click(object sender,EventArgs e) { for (int i = 0; i <= HighChan; charts[i++].Series.SuspendUpdates()) ; t = new System.Threading.Thread(updatePlot); t.Start(); //run timer to get new data with tim.interval tim.Stop(); tim.Elapsed += new ElapsedEventHandler(this.OnTimedEvent); tim.Start(); } bool entry = false; private void OnTimedEvent(object sender,array wit 16 fields entry = true; } void updatePlot() { while(true) { if(entry) { charts[0].Invoke(new Action(delegate() { for (int i = 0; i < HighChan + 1; i++) { //charts[i] stuff here } } entry = false; } } }
这些解决方案只取得了很小的改进.不幸的是,在这两种情况下,图表都没有足够快地刷新.此外,来自charts []阵列的前8个快速刷新,而后8个帧速率约为0.5 Hz,对我而言,它们的行为方式并不相同.
更新2:
和第一篇文章一样,我需要每20-40分钟重绘一次图表.当我只绘制一个绘图时,我可以达到此帧速率,因此不需要数据采集时间(A / C在背景中工作,Fs = 1k Hz).也许绑定队列到图表使它更快但不是很多.
我将更准确地描述我所进行的性能测试中发生了什么.
因此,当我将nr(要刷新的图表数量)限制为6时,用ti(时间间隔)20ms更新它们,因此它们一次存储点数(np)= 50,它们都运行平稳.但是,当我将np更改为100时,只有图表1-4运行平稳,5表示非常慢,6表示停止.
当nr = 6,ti = 20,np = 250时,图表1-3运行平滑,而图表4-6运行为0.1fps.
当nr = 10,np = 50时,图表1-6的行为与nr = 6的情况相同,而图表7-8令人惊讶地运行平稳,9-10表示像1fps.
当nr = 10,np = 100时,图表1-6的行为与nr = 6的情况相同(因此只有图表1-3运行平滑),而图表7-8仍然运行平稳且9- 10就像0.1fps.
当nr = 16,图1-8的行为类似于nr = 10的情况,而其他8的情况类似于0.05fps或其他.
无论我使用哪种方法,单线程,多线程,单/多调用,队列绑定,Thread.Sleep(),Threed.CurrentThread.Join(),Timers,async结果总是更不一样.老实说,我不知道为什么我无法在16个fastline图表上获得实时数据预览.
我会感谢任何建议.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。