Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

"Handle is not initialized" vb.net

I am getting an intermittant  "Handle is not initialized" after EndReadString() with vb.net 2005.
 
Have I forgotted to do something or done something I shouldn't have?
 
Main lines of code:
Private Sub Sub_Main()

'Open
mbSession =
CType(ResourceManager.GetLocalManager().Open(Resource), MessageBasedSession)
mbSession.SynchronizeCallbacks =
True

' Setup Callback
asyncHandle = mbSession.BeginRead(mbSession.DefaultBufferSize,

New AsyncCallback(AddressOf OnAsyncReadComplete), Nothing)

'Close
mbSession.Dispose()
mbSession =
Nothing

End Sub

'Callback Sub
Private Sub OnAsyncReadComplete(ByVal result As IAsyncResult)
          Dim strStuff as String = mbSession.EndReadString(IAsyncResult)
         
'Do stuff with strStuff
     
End Sub

 

0 Kudos
Message 1 of 4
(4,631 Views)
Hi,

Just quickly looking at the code you posted, I believe you will want to use mbSession.EndReadString(result) instead of mbSession.EndReadString(IAsyncResult).  I would recommend checking out our examples for doing simple asynchronous reads and writes.  It is located at C:\Program Files\National Instruments\MeasurementStudioVS2005\DotNET\Examples\Visa\SimpleAsynchronousReadWrite, or it might also be located at C:\Program Files\National Instruments\MeasurementStudioVS2003\DotNET\Examples\Visa\SimpleAsynchronousReadWrite.  Hope this helps.
Pat P.
Software Engineer
National Instruments
0 Kudos
Message 2 of 4
(4,609 Views)
Thanks Pat, I wondered when someone would spot that, but alas, one can't edit or delete ones post 😉
 
In my defence, I did post at about 3am our time 🙂
 
As it so happens, I am using result and here is the corrected skeleton code:
 
Private Sub Sub_Main()

'Open
mbSession =
CType
(ResourceManager.GetLocalManager().Open(Resource), MessageBasedSession)
mbSession.SynchronizeCallbacks =
True

' Setup Callback
asyncHandle = (mbSession.BeginRead(mbSession.DefaultBufferSize, New AsyncCallback(AddressOf OnAsyncReadComplete), Nothing))

'Close
mbSession.Dispose()
mbSession =
Nothing

End Sub

'Callback Sub
Private Sub OnAsyncReadComplete(ByVal result As
IAsyncResult)
          Dim strStuff as String = mbSession.EndReadString(result)
         
'Do stuff with strStuff
 
End Sub

Now then, It all works very well if I close and re-open the session each time I do a read.

However, is it supposed to behave like that?

Shouldn't I just have to open session once?

Also, this adds too much latency from the time you want the code to read and the time it actually does the read.

 

 

0 Kudos
Message 3 of 4
(4,602 Views)
Hey Mark,

You shouldn't have to close and reopen the session each time.  You should be able to open the session, leave it open, and call begin read multiple times to read on the same session.  In looking at your skeleton code, one potential problem I see is in your close section of code.  Is this actually located in the same function as the open and setup callback code?  Because you will not want to close your session until you have finished reading.  That is really the only potential problem I see.  If your program isn't too large could you possibly post the project you are working on?


Pat P.
Software Engineer
National Instruments
0 Kudos
Message 4 of 4
(4,583 Views)