Windows 表单计时器未按时执行

Windows Forms Timer not executed on time

我在我的应用程序中使用 System.Windows.Forms.Timer,它会触发一种方法,将新数据添加到四个 LiveCharts。间隔设置为 1000 毫秒。它在前一两分钟内工作正常,但随后间隔变得越来越大,10 分钟后它仅每 15 秒左右触发一次。 我尝试过使用不同的计时器但没有任何运气。可能是 application/LiveCharts 的性能太差了,因为 UI 线程上的计时器 运行s 它必须等待应用程序 "ready"?

谢谢!

定时器设置

graphRefreshTimer = new System.Windows.Forms.Timer();
graphRefreshTimer.Interval = 1000;
graphRefreshTimer.Tick += new EventHandler(refreshUI);
graphRefreshTimer.Start();

刷新UI(基本上只是将新的 ChartValues 添加到 LiveCharts。最多需要 100 毫秒到 运行)

    //...
B1VoltageValues.Add(new MeasureModel
                {
                    DateTime = now,
                    Value = Convert.ToDouble(B1.Voltage) / 1000
                });

B1CurrentValues.Add(new MeasureModel
                {
                    DateTime = now,
                    Value = Convert.ToDouble(B1.Current) / 1000
                });

B1ChargingCapacityValues.Add(new MeasureModel
                {
                    DateTime = now,
                    Value = Convert.ToDouble(B1.getChargeCapIfTrue()) / 1000
                });

B1DischargingCapacityValues.Add(new MeasureModel
                {
                    DateTime = now,
                    Value = Convert.ToDouble(B1.getDischargeCapIfTrue()) / 1000
                });

B1EnergyValues.Add(new MeasureModel
                {
                    DateTime = now,
                    Value = Convert.ToDouble(B1.Energy) / 1000
                });
//...

问题的解决方案是使用 LiveCharts.Geared 包,它可以处理比标准包更多的数据。使用此包后 UI 没有进一步冻结。