我正在制作SCADA屏幕(c#wpf)。我有很多动画/文本框和标签更改等。但是主要问题是关于我的图表(Winform图表)。我必须每2秒更新一次(它有8行)。因此,一段时间后,它会使ui线程无响应。
对于更快的UI线程我该怎么做?
private static BackgroundWorker backgroundWorker;
private void window_Loaded(object sender, RoutedEventArgs e)
{
backgroundWorker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
backgroundWorker.DoWork += backgroundWorker_DoWork;
backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int i = 0;
while (counter >= 1)
{
i++;
//CHECK FOR CANCELLATION FIRST
if (backgroundWorker.CancellationPending)
{
//CANCEL
e.Cancel = true;
}
else
{
simulateHeavyJob();
backgroundWorker.ReportProgress(i);
}
}
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e){
Random x = new Random();
int ajn = x.Next(0, 100);
if (Randıman0.active)
{
cpuChart.Series[Makineler[0].isim].Points.AddXY(DateTime.Now, ajn-5);
if (cpuChart.Series[Makineler[0].isim].Points.Count > 1800)
cpuChart.Series[Makineler[0].isim].Points.RemoveAt(0);
}
if (Randıman1.active)
{
cpuChart.Series[Makineler[1].isim].Points.AddXY(DateTime.Now, ajn+5);
if (cpuChart.Series[Makineler[1].isim].Points.Count > 1800)
cpuChart.Series[Makineler[1].isim].Points.RemoveAt(0);
}
if (Randıman2.active)
{
cpuChart.Series[Makineler[2].isim].Points.AddXY(DateTime.Now, ajn+10);
if (cpuChart.Series[Makineler[2].isim].Points.Count > 1800)
cpuChart.Series[Makineler[2].isim].Points.RemoveAt(0);
}
if (Randıman3.active)
{
cpuChart.Series[Makineler[3].isim].Points.AddXY(DateTime.Now, ajn-10);
if (cpuChart.Series[Makineler[3].isim].Points.Count > 1800)
cpuChart.Series[Makineler[3].isim].Points.RemoveAt(0);
}
if (Randıman4.active)
{
cpuChart.Series[Makineler[4].isim].Points.AddXY(DateTime.Now, ajn+20);
if (cpuChart.Series[Makineler[4].isim].Points.Count > 1800)
cpuChart.Series[Makineler[4].isim].Points.RemoveAt(0);
}
if (Randıman5.active)
{
cpuChart.Series[Makineler[5].isim].Points.AddXY(DateTime.Now, ajn-20);
if (cpuChart.Series[Makineler[5].isim].Points.Count > 1800)
cpuChart.Series[Makineler[5].isim].Points.RemoveAt(0);
}
if (Randıman6.active)
{
cpuChart.Series[Makineler[6].isim].Points.AddXY(DateTime.Now, ajn+15);
if (cpuChart.Series[Makineler[6].isim].Points.Count > 1800)
cpuChart.Series[Makineler[6].isim].Points.RemoveAt(0);
}
if (Randıman7.active)
{
cpuChart.Series[Makineler[7].isim].Points.AddXY(DateTime.Now, ajn-15);
if (cpuChart.Series[Makineler[7].isim].Points.Count > 1800)
cpuChart.Series[Makineler[7].isim].Points.RemoveAt(0);
}
}
private void simulateHeavyJob()
{
Thread.Sleep(2000);
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
/*
display("You have Cancelled");*/
}
else
{
// display("Work completed successfully");
}
}
我的图表每隔一分钟也向左滑动一次:
Dispatcher.Invoke(() => { cpuChart.ChartAreas[0].AxisX.Minimum = minDate.ToOADate(); });
Dispatcher.Invoke(() => { cpuChart.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate(); });