From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabView DLL hangs when called from C

I've got a collection of LabView VIs that runs fine when executing the source code but when I build thme into a DLL and call it from a C application it hangs. Two calls are made into the DLL, the first is from main() and the second one is called from a task spawned by main(). The second call always hangs when called in this manner. Just for grins I moved the second call into main() and called it shortly after the first call into the DLL. This case does not hang. Does anyone have some insight into why this is happening?

Thanks!
0 Kudos
Message 1 of 6
(2,479 Views)
It depends on what the main thread is up to, but from the description my guess is that you are not pumping messages.

The first thread (main) goes into LabVIEW and probably opens up a front panel or does some other UI action. While the thread is inside the LV runtime, LV makes sure that OS messages are pumped. However, once that thread returns out of the runtime it is up to you to pump the messages. Then when the second thread tries to enter LV, it is probably blocking on some code that is waiting for one of those messages.
0 Kudos
Message 2 of 6
(2,474 Views)
Brian,

Thanks for the info. I'm not clear on what you mean by pumping messages, could you elaborate on that a bit for me?

Thanks

Brian
0 Kudos
Message 3 of 6
(2,469 Views)
It's a UI concept - Windows UI is based off of events (Mouse Up, Key Press, etc) that are routed to the window in focus. However, for many reasons that I won't go into here, it is a single threaded type of system - so there must be a thread whose job it is to listen for these events to arrive and then turn around and deliver them (this is known as message pumping).

Now, how to add message pumping to your C application? Well a simple little loop you can call is

while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

This while loop will end when all messages have been dispatched, but more are going to be coming in, so if your main thread is doing something else, you'll need to do the loop, do a bit of work, do the loop, do a bit of work...

A common solution is to create a thread in your application and make sure it is the first one to call into LV. Then that thread just sits forever doing a loop. Of course, in that case, you should change PeekMessage to GetMessage.
0 Kudos
Message 4 of 6
(2,464 Views)
Brian,

Thanks for the explanation of message pumping, I do vaguely recall this from a little Windows programming I did in the past. I am curious what types of messages are being sent. Are there any non-GUI related messages? Is the LabView run-time engine pumping messages to an invisible front panel?

Thanks

Brian
0 Kudos
Message 5 of 6
(2,445 Views)
That is a question that would take a long time to go through (and gets into some internal implementation issues). Lets just say that it is best to ensure that if you return from the RTE and expected either (a) VIs to keep running and displaying data and/or (b) call into the RTE from a different thread, you need to pump.
0 Kudos
Message 6 of 6
(2,438 Views)