Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

.Net graph component exception

Hello,
We are currently evaluating the Measurement Studio .Net graph component. We are getting exception when using this control in our application (please see attached file). This exception happens most often when using axis mode StripChart, but similar exception also happens when using ScopeChart.
We have 4 plots and we are adding new data approx. each 50 ms; 2 of the plots get 1 point and 2 others get 10 points each 50 ms. We tried to make this period of time longer but this eventually leads to the same exception. All data is added from one thread.
When we switched to older ActiveX graph - it works ok.
Please help ASAP - we need to make a decision about purchase of measurement studio for our project.
Thanks.
0 Kudos
Message 1 of 4
(3,595 Views)
How are you plotting data on an interval? Via a timer? If so, which timer? System.Windows.Forms.Timer or System.Timers.Timer? Could you please post a small test project that demonstrates this behavior? Thanks.

- Elton
0 Kudos
Message 2 of 4
(3,589 Views)
Hello Elton,
attached please find a test project that demostrates the problem. I decreased the intervals between updates to the graph in order to get the exception more consistently. Also, this time I get another exception but the stack trace is very similar to the previous one.
Thanks.
0 Kudos
Message 3 of 4
(3,566 Views)
The problem that you're seeing is that you're creating a new thread and updating the UI in this separate thread, but Windows Forms controls are not thread-safe. Notice that the class overviews of all .NET Framework and Measurement Studio Windows Forms controls have a thread safety section that states:


"Only the following members are safe for multithreaded operations: BeginInvoke, EndInvoke, Invoke, InvokeRequired, and CreateGraphics."


Here are a few online resources that provide more detail about using Windows Forms controls with multi-threaded applications:



Here's an example of how to use Invoke with the code that you posted so that the UI updates happen on the UI thread and do not result in an exception:


private void btnStart_Click(object sender, System.EventArgs e) {
StartTest();
}

private void StartTest() {
thread = new Thread(new ThreadStart(SimulationThread));
thread.IsBackground = true;
thread.Start();
}

delegate void PlotDoubleTimeSpanHandler(WaveformPlot plot, double data, TimeSpan interval);
delegate void PlotDoubleArrayTimeSpanHandler(WaveformPlot plot, double[] data, TimeSpan interval);

void PlotDoubleTimeSpan(WaveformPlot plot, double data, TimeSpan interval)
{
plot.PlotYAppend(data, interval);
}

void PlotDoubleArrayTimeSpan(WaveformPlot plot, double[] data, TimeSpan interval)
{
plot.PlotYAppend(data, interval);
}

void UpdateGraph(Delegate handler, params object[] args)
{
graph1.Invoke(handler, args);
}

private void SimulationThread() {
try {
TimeSpan ts5 = new TimeSpan(0, 0, 0, 0, 5);
TimeSpan ts45 = new TimeSpan(0, 0, 0, 0, 45);
DateTime lastTime = DateTime.Now;

PlotDoubleTimeSpanHandler valueIntervalHandler = new PlotDoubleTimeSpanHandler(PlotDoubleTimeSpan);
PlotDoubleArrayTimeSpanHandler arrayIntervalHandler = new PlotDoubleArrayTimeSpanHandler(PlotDoubleArrayTimeSpan);

while(true)
{
DateTime now = DateTime.Now;
TimeSpan timeIncr = now - lastTime;
lastTime = now;

// plot1.PlotYAppend(1, timeIncr);
UpdateGraph(valueIntervalHandler, plot1, 1, timeIncr);

// plot1.PlotYAppend(new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1}, ts5);
UpdateGraph(arrayIntervalHandler, plot1, new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1}, ts5);

// plot2.PlotYAppend(2, timeIncr);
UpdateGraph(valueIntervalHandler, plot2, 2, timeIncr);

// plot2.PlotYAppend(new double[] {2, 2, 2, 2, 2, 2, 2, 2, 2}, ts5);
UpdateGraph(arrayIntervalHandler, plot2, new double[] {2, 2, 2, 2, 2, 2, 2, 2, 2}, ts5);

// plot3.PlotYAppend(3, timeIncr + ts45);
UpdateGraph(valueIntervalHandler, plot3, 3, timeIncr + ts45);

// plot4.PlotYAppend(4, timeIncr + ts45);
UpdateGraph(valueIntervalHandler, plot4, 4, timeIncr + ts45);

//Thread.Sleep(50);
Thread.Sleep(25);

}
}
catch(Exception e) {
Console.WriteLine(e);
}
}


- Elton
0 Kudos
Message 4 of 4
(3,525 Views)