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: 

UnsafeInvocationException

Hi,

 

First of all Happy New Year for everyone.

 

I am playing with Measurement Studio and NIDAQmx.  I have implemented the following soultion in C# : http://zone.ni.com/reference/en-XX/help/370473H-01/mstudiowebhelp/html/daqsafelydisposetask/

 

In this example 

analogReader.SynchronizeCallbacks = false;

so the DAQ is running on any thread. When debugging I see the callback is executed on different threads, moreover the thread.MangedID is changing randomly. 

 

I have extended the above example with some buffering functionality,

ChartCollection<double>[] _graphData = new ChartCollection<double>[4];

where _graphData is sent to the callback method as object paramter in order to append the most recent acquired data and this collection is set as DataSource to NIChartControl. 

(This collection is created on the main thread.)

 

When I set 

analogReader.SynchronizeCallbacks = false;

I am getting exceptions like UnsafeInvocationException, TargetInvocationException and "The data source was modified"

 

When I read this info: http://zone.ni.com/reference/en-XX/help/372636F-01/mstudiowebhelp/html/e3ef07a6/ I am assuming there is a thread-affinity behind the scenes.

 

Here is my ProcessData method

 

// ProcessData method executes on the same thread on which AnalogReadCallback executes in our case on WorkerThread
private void ProcessData(double[,] data)
{

Debug.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

        for (int i = 0; i < 4; i++)
                Buffer[i].Append(data[i, 0]);
           
}

 

So I think the problem is when I call the Apped method that fires the DataChanged event which runs on the Main Thread. 

 

What do you think?

 

My other question: Is there any way to prevent firing DataChanged event every interation in the for loop? Only the last call should fire the event.

I need something like DeferPanel = TRUE 🙂 similar in LabVIEW. 

 

Looking forward any reply!

Thank you!

 

 

 

---
+++ In God we believe, in Trance we Trust +++
[Hungary]
0 Kudos
Message 1 of 1
(5,342 Views)