Streaming with U3 "StreamWindowed" "Cross-thread operation not valid" | LabJack
 

Streaming with U3 "StreamWindowed" "Cross-thread operation not valid"

4 posts / 0 new
Last post
wyn
wyn's picture
Streaming with U3 "StreamWindowed" "Cross-thread operation not valid"

Hello, 

I am trying to write a program that will be able to stream data from the labjack. I started by using your example code and was able to run simple stream but am not able to run "U3_StreamWindowed" found in "DotNet_LJUD\Examples\LJUDDotNet\U3\C#\U3_StreamTimerWindow" (I have not made any changed to the example code). Everytime I attempt to run it I get the same error "Cross-thread operation not valid: Control 'ain0Display' accessed from a thread other than the thread it was created on." I have never messed around with threads or streaming so was really hoping for some clarification as to how to fix this error. 

Thank you for your time, 

Wyn 

LabJack Support
LabJack Support's picture
We want GUI updates to be run

We want GUI updates to be run by the GUI/Windows Form thread. Currently, the timer in the example does not have any synchronization object defined, so the Elapsed time event (which updates the GUI) can be called by a worker thread instead of the Windows Form thread. This may be an oversight due to changes in .NET since we created the example.

To fix the issue, you can define the synchronizing object for the timer as the StreamForm object by adding the following line after the updateTimer constructor around line 88 in StreamWindowed.cs:

updateTimer.SynchronizingObject = this;

For more information on the timer synchronization object property please see the following docs:

https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer.synchron...

 

wyn
wyn's picture
Thank you for your time it

Thank you for your time it worked!

Unfortunaly I have a slightly different streaming issue now. Long story short I would like to have a sequence in NI Test Stand that will stream mesurments from the labjack and return an array of those mesurments for later analysis. I am getting some weird errors with the program I created so I tried to turn your example code "U3_SimpleStream.cs" into a dll and just call performActions(). in Test Stand. I got the same error I did with my own code (picture of said error attached). The program works perfectly when I run it as just a .Net C# file outside of Test Stand. 

I know this is most likely a Test Stand problem but I would really appracate any advice you can give. 

 

File Attachment: 
LabJack Support
LabJack Support's picture
The issue is likely to do

The issue is likely to do with the eGet call itself if TestStand is a 64-bit application. Please try to replace 'eGet' with 'eGetPtr' on the line that throws an exception (or any other eGet calls that pass an array of values). There is a similar issue with ePut and AddRequest; if you pass an array to either of those functions anywhere they should be changed to ePutPtr or AddRequestPtr as well.

The issue is that the x1 parameter for eGet is normally a 32-bit integer, and if you pass an array you are passing a pointer or reference to the array to x1. The array reference should be a 64-bit address in a 64-bit app, so you are trying to pass a 64-bit address, cast as a 32-bit value, hence the exception. The eGetPtr function uses a void* for x1 to resolve this issue.

This behavior is documented in our function references, and we recently added warnings to our example code pages to reflect this limitation. We also plan to update all of our examples where this could be an issue in 64-bit apps:

https://labjack.com/support/software/api/ud/function-reference/eget-and-...

https://labjack.com/support/software/examples/ud/dotnet