根据您的描述,您希望使用 100ms 的计时器并避免在使用上述代码时锁定控件。
我建议你可以使用计时器来做到这一点。
这是您可以参考的代码示例。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Dictionary<double, double> dic = new Dictionary<double, double>();
private void Form1_Load(object sender, EventArgs e)
{
chart1.Series.Clear();
var series1 = new System.Windows.Forms.DataVisualization.Charting.Series
{
Name = "Series1",
Color = System.Drawing.Color.Green,
IsVisibleInLegend = false,
IsXValueIndexed = true,
ChartType = SeriesChartType.Line
};
this.chart1.Series.Add(series1);
series1.Points.AddXY(1, 10);
series1.Points.AddXY(2, 14);
chart1.Invalidate();
timer1.Interval = 100;
dic.Add(1, 20);
dic.Add(2, 30);
dic.Add(3, 40);
dic.Add(4, 60);
dic.Add(5, 70);
}
private void timer1_Tick(object sender, EventArgs e)
{
chart1.Series[0].Points.Clear();
// Get data from sensor using sensor sdk,
// The function returns 2 arrays, x-array and y-array of values to be plotted
// Display x and z values
chart1.Series[0].Points.DataBindXY(dic.Keys, dic.Values);
chart1.Update();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
}
结果: