LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Dotnet Callback function without events

Solved!
Go to solution

Hi,

 

I have a dot net dll I am accessing from labview. One of the features is to setup a callback function by passing in the function name to the dll. 

 

Here is c# pseudo code from their example. 

 

camera.SetCallbackFunction(userSuppliedFunction)

public static void userSuppliedFunction(int imageCount, int bufferNum, CameraObject_NET cameraobject)

{ //this is the callback function 
}

 

I have tried everything I could think of to implement the above in labview. My best bet was to use a static vi reference as the supplied callback function. The problem is is that the dll setcallbackfunction doesnt seem to accept anything I wire to it. It accepts a ".NET refnum" as an argument. I have tried to convert the static VI to a .NET refnum but this simply crashes. 

 

Any help is appreciated. Thank you.  

0 Kudos
Message 1 of 2
(3,161 Views)
Solution
Accepted by topic author NiaccountAndrew

When you pass a callback function what you are actually passing is a .NET Delegate that targets that function (and the object reference if the callback method is an instance method). C# has some syntatic sugar to make this easier to read but basically your "SetCallbackFunction" method is requiring a delegate with a specific signature  (void return, parameters int, int and CameraObject_NET types). Whether this is an instance of a  custom delegate (defined in C# using the delegate keyword) or a delegate with the right signature in the BCL (eg. Action(T1, T2, T3)) is hard to tell from your code.

 

This is why LabVIEW is expecting a ".NET refnum" - the method is expecting a reference to a delegate that has a pointer to the method to call.


But either way - LabVIEW doesn't currently support creating instance of delegates directly (see here). LabVIEW does support providing callbacks as event handlers to events via the Register Event Callback node which you can then create and attach a callback VI.

 

Check to see if the API you are using has an equivalent event syntax for what you are trying to do. If it does not then your only other option is to create a "adaptor" class in .NET that can expose the original API in a syntax more connectable in LabVIEW eg. an event that will set the handler provided in the adaptor class.

 

Either way be very careful what you put in your callback function - be aware that it isn't LabVIEW executing your code when the callback executes - it is whatever thread uses the callback function; probably buried inside that "camera" assembly. Try and keep the logic in the callback short and simple and minimise blocking operations.

 

Message 2 of 2
(3,130 Views)