I am trying to do the live plot of a sensor data.The UI consists of a chart, a start and stop button.
When the start button is pressed, the data from sensor is plotted on the chart using a timer of 100ms. But it throws an exception like System Execution Engine exception. There is no exception or other issues if I replace the timer way of updating values with a while(true) loop.But in that case I will lose my control over the other parts of UI, as I am using only 1 thread.
Suggestions / opinions / help welcome!
while (true)
{
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(adValueX, adValueZ);
chart1.Update();
System.Threading.Thread.Sleep(100);
}
When using while (true)
in your UI thread, you're basically blocking all other UI related functionality (because the UI thread is busy).
There are 3 common way's to overcome this problem:
Application.DoEvents()
to your loop, which is actually a hack to overcome the UI responsiveness.You already used this last option, and hit an error. Most likely your Timer was not running on the UI thread, which can cause problems while updating UI components.
There are various ways to fix it: here's one:
protected void OnYourTimerEventHandler()
{
BeginInvoke(new MethodInvoker(delegate
{
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(adValueX, adValueZ);
chart1.Update();
}));
}
More documentation can be found on MSDN