LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

C# Async based dll in LabVIEW

Solved!
Go to solution

Target Client - LabVIEW 2014

 

Hi,

 

I have created a DLL of TAP() in C#. I wish to consume my async functions in LabVIEW, is it possible? If yes how?

 

My function looks like :-

Snippet

public async Task<bool> AttachCOMPortAsync(string portName)
{
// Do something
}

 

0 Kudos
Message 1 of 3
(5,038 Views)
Solution
Accepted by topic author Novoice

I assume you've at least tried it. What I can tell you is:

  • Task<T> closed types and the TPL in general can be accessed from LabVIEW as long as you only use methods and types that work within the limitations here
  • Async and Await C# keywords are a special construct of the C# language itself and not part of the CLR or the compiled IL. The C# compiler builds the state machine for you and handles the execution context data so that parts of the state machine can be executed by the relevant threads (eg. the original caller, often the UI) transparently. You still have Task/Task<T> working for you in the background.
  • The nature of the state machine means that when parts of the code need to be executed again by the calling thread that this code is passed to the calling thread via an instance of SynchronisationContext and windows messaging in the background. This won't work properly (I suspect not at all) with LabVIEW so continuations on the calling thread will be a no-no.
  • This all implies that the async/await C# semantics will not help much when used for interop with LabVIEW. You would get no improvement over just returning a Task<bool> in your example and then, at some point in your LV code, polling for a result on that Task (or wiring up a delegate).
0 Kudos
Message 2 of 3
(4,997 Views)

@tyk007

 

Thanks for the reply.

I did try out before posting this question to forum. Also that was my day 1 with LabVIEW so I myself wasn't aware of many things. So for the reasons you have mentioned I believe that Task<T> types with async won't work (However from the link you posted I don't think limitation of LabVIEW 2014 specifically are pronounced evidently).

So I wrapped them as synchronised functions with AsyncContext.Run and that seems to do the job though. Later I went on to use Events in my C# library and used Event callback (basically raised event in dll whenever async task was finished) in LabVIEW and that too worked.

0 Kudos
Message 3 of 3
(4,977 Views)