Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

SimpleAsynchronousReadWrite fails to read

I am trying to implement asynchronous read/write using C# .NET.  I downloaded and compiled SimpleAsychronousReadWrite.sln.  The Write method works fine.  However, if I click on Read, I always receive the error message: "Cross-thread operation not valid: Control "stringRead" accessed from a thread other than the thread it was created on."  This under Visual Studio 2005... could that be the problem ?  I'm stuck and would appreciate any pointers.

0 Kudos
Message 1 of 4
(3,476 Views)
I found the answer to my question.  Visual Studio 2005 is more stringent than VS 2003 in terms of access to GUI components.  You can't let any thread except the main thread try to access GUI components such as text boxes on the main form.  I understand that this is a better programming practice, but it is also a huge pain.  NI-488.2 spawns its own thread(s?) so it cannot include commands like:

stringRead.Text = InsertCommonEscapeSequences(device.EndReadString(result)); 

So... what to do?  Hit Google, of course.  Here's a solution that I found: http://dotnetjunkies.com/WebLog/johnwood/archive/2005/08/31/132267.aspx  It works well even though I found it difficult to understand.

Bottom line, the examples such as SimpleAsynchronousReadWrite will not work under VS 2005 unless they are modified.
0 Kudos
Message 2 of 4
(3,463 Views)
If you want something simple that does not require using reflection, you can review this link here.

Basically, all you need is to check is if the call is in a non-UI thread (InvokeRequired) and if it is, use a delegate and call that delegate with Invoke().

Could you post the link to where you found the example? If there is a problem with it, we'll need to update it.

Thanks.


Bilal Durrani
NI
0 Kudos
Message 3 of 4
(3,457 Views)
Thanks - delegates are a good alternative to reflection.

The examples that I was using were from the NI web site
ftp://ftp.ni.com/support/mstudio/MStudio488API-%20For%20DotNET%202003/

Also... the Help doesn't install into VS 2005 either. 

Thank you for the terrific support.
0 Kudos
Message 4 of 4
(3,452 Views)