Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

How to unlock a resource if the locking session has failed, in C#

Greetings.
 
I am developing an application that uses the .NET NI-VISA library, using the C# programming language.
I am talking to instruments using Message Based Sessions. Al is well with this until either an unhandled exception
occurs, or i inadvertently stop debugging. At this point, the VISA Sessions are still open and locked. If I run
my application again, I get the following exception:
 

NationalInstruments.VisaNS.VisaException: Specified type of lock cannot be obtained, or specified operation cannot be performed, because the resource is locked. VISA error code -1073807345 (0xBFFF000F), ErrorResourceLocked

 

Is there a way for me to programmatically unlock or close all VISA sessions on my machine, without knowing the handles

to the sessions in advance?

 

Thanks.

0 Kudos
Message 1 of 3
(7,759 Views)
Hi,
  the VISA server itself is handling the handles internally, so if you close off without shutting off specifically, then unfortunately you're going to get into that kind of a problem and there's no programatic "clear all refs" option.
 
The only option is to remove all reference from the visa server itself by shutting down the development environment. (I'm assuming that if you do that, it seems to work ok afterwards or do you require a reboot of the machine to clear the lock?)
 
Another architecture could work though, where you make a wrapper around the visa calls, and that stays permanently running, that way if you need to, you could write your own clear all, since the handles in that dll or whatever it is that's compiled, linked and running separately would still have valid handles (so you're not going to debug it and stop it prematurely)
 
In terms of unhandled exceptions, then program with the try...catch...finally methodology so you can make sure you're releasing references appropriately :
 
Hope that helps
 
Sacha Emery
National Instruments (UK)
 
// it takes almost no time to rate an answer Smiley Wink
0 Kudos
Message 2 of 3
(7,748 Views)

Hi,

 

I am facing this issue too with NI Visa version4.6.2

Shutting down the development env manually can solve the problem but it seem like not realistic in my application.

For the wrapper solution, it can't work.

S0, is there any other automated ways to remove all the reference from the server when this exceptional error being catched?

 

Thanks,

Ai Heong

0 Kudos
Message 3 of 3
(6,885 Views)