From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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: 

Detect when a sub-VI has finished running

Hi everyone. 

 

I have been trying to solve this all day, but still haven't managed.

 

I have a little sub-VI that takes in some numeric data, processes it and sends it to the calling VI. Now, the things is that when this sub-VI gets called, the rest of the program stalls, since it is waiting for the output from the sub-VI. I need, however, the program to keep sending data, so some default data must be sent while the program waits for the user to finish executing the sub-VI. So, I need to set up the VI to run asynchronously, so it can run on the background while the main VI still executes.

 

The problem is, however, that I still do not know how to detect when the sub-VI has finished running, so that I can send the output data from it rather than the default. I have tried the 'Execution State' property node, but at soon as the sub-VI is run the first time, it shows 'Running', even when the sub-VI is stopped. 

 

I would really appreciate your help.

0 Kudos
Message 1 of 12
(5,468 Views)

running.png

 

You're doing something like this I assume and it doesn't work?

0 Kudos
Message 2 of 12
(5,459 Views)

A slightly different approach for you to consider:

 

You could set up a notifier between your main VI and your sub VI.  The data that the notifier contains is the "run" status of your subVI (say, a boolean or an enumerated value).  When your notifier returns a "finished" value (say, your boolean goes T or your enumerated value returns a value of "Stopped"), your main VI will know that the subVI has finished running.

 

You'll want a timeout value on the "Wait on Notification" function in your main VI, so that your main VI will continue to execute while it is waiting for the subVI to finish.  If you don't wire a timeout value, "Wait on Notification" will just sit and wait until it receives a notification, causing your main VI to sit and wait too, which is specifically what you're trying to avoid.

 

Hope that helps.

0 Kudos
Message 3 of 12
(5,435 Views)

You could also use a user event which the subVI will fire when it exits. This won't help you at the moment but you could vote for this idea in the idea exchange as well.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 12
(5,422 Views)

Sounds like you need a producer and consumer loops with feedback.  Typically such an architecture would use Queues.

 

Your program would have two loops, one with the main process and one with the subvi.  Create 2 queues, one to feed data to the sub-vi, and one to feed the results back to the main loop.

 

You can set the Dequeue Element timeout to something small, say 50-100ms.   Trap the error and either do nothing or crunch the default data as desired.  I'll try and post an example later if no one beats me to it.

 

Adam

0 Kudos
Message 5 of 12
(5,414 Views)

A good reason to upgrade to 2011 if you have not already. It has a new feature - Asynchronous Call By Reference.

=====================
LabVIEW 2012


Message 6 of 12
(5,407 Views)

Thanks everyone for your answers. 

 

Steve, I am using Labview 2011 and the 'Start of Asynchronous Call' VI. I have tried with a few different sub-VIs and found an issue which I'd appreciate if someone could clarify for me. When the asynchronously-called sub-VI is written in a while loop (such in my case, where the sub-vi keeps looping until the user decides which data to send), execution in the main VI stalls. Is there a way of getting round this?

 

Diane, your idea is exactly what I had thought, but I am not sure if I managed to implement in the best way. I tried wiring a Boolean indicator to the termination control of the while loop within which my sub-vi is contained. If I set up the asynchronous call in main within a Flat Stacked Sequence where the first frame sets the termination to false and then calls the sub-vi asynchronously, it seems to work fine, but I am not sure if this is the best way to do it. However, the problem with the main vi stalling still exists.

 

Thanks a lot,

 

Gabriel

0 Kudos
Message 7 of 12
(5,378 Views)

We would need to see your code in order to answer your question.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 8 of 12
(5,356 Views)

If I understand what you are trying to accomplish you might be able to do this with two loops. One of the loops could be in a SubVI. You just need to communicate between the two. You can use notifiers, queues or user events (there are other ways besides my favorite three)

 

Here is an example that shows how to spawn a long running task without blocking the main loop. It uses a notifier to send from the "main" loop to the "task" loop. When the long running task is done it tells the main loop via a user event. The reason for using a user event instead of another notifier or something is simply because that loop already has an event structure.

 

loop communication_BD.png

 

Now I am going to write another post with this snippet in it. There is a very strange thing happening. Here is the link for that

=====================
LabVIEW 2012


0 Kudos
Message 9 of 12
(5,339 Views)

I don't have a good feel for what your code looks like.

 

As I understand it, you have a subVI inside a while loop, inside your main code.  You want to know when this subVI finishes executing.  Is that correct?

 

If so, then where have you placed your notifier?  Inside the subVI, or inside your while loop?  Placing it inside your while loop doesn't make a lot of sense to me, unless you are indeed using a producer-consumer architecture and are notifying a parallel loop that the loop containing the subVI has terminated. 

 

I believe that I (and others!) will be able to help you better if you can post your code, or a small example which demonstrates the problem.  I don't quite understand what it is you're trying to do.  Do you want to run your subVI repeatedly inside a loop, and notify your main program each time the subVI finishes running (in which case the notifier needs to be inside the subVI), or do you want to notify your main program when the loop containing the subVI finishes running (in which case your notifier can be inside the while loop containing your subVI)?  They are two different things (unless, of course, your while loop only runs one time and then stops).

 

 

0 Kudos
Message 10 of 12
(5,328 Views)