From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Why Invoke a delegate rather than simply calling the method?

Hi

I am looking at the NetworkVariable example that ships with MS 8.1 Professional, "Basic.2005".

In the BufferedWriter.2005 project, to plot values to the WaveformGraph the following code is used:
Invoke(new EventHandler(delegate{ sampleWaveformGraph.PlotYAppend(values); }));

Why don't you simply call the method on the instance of the WaveformGraph?:
sampleWaveformGraph.PlotYAppend(values);

Thanks

Adam

0 Kudos
Message 1 of 2
(6,147 Views)
The WriteData method is executing in a worker (non-UI) thread. Windows programming prohibits updating UI controls from a thread other than the one that created the UI in the first place. By using Invoke, we make sure that the PlotYAppend method executes in the thread that owns the sampleWaveformGraph. See the help for Invoke for more information. Not doing this would cause the application to throw weird errors, even though it may seem things are working in the beginning.


Hope this helps.
Bilal Durrani
NI
Message 2 of 2
(6,113 Views)