LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Stop VI and a Sub VI together

Solved!
Go to solution

Hi,In my application i have a vi with a state machine  having a event handler loop.One of the events in my event handler loop is exit which directs it to the exit state of my state machine.I also have a sub vi outside my statemachine.

The problem faced:

1.On pressing exit my Vi does not stop .

Reason is:My sub vi is still running.

Solution Needed :How tp stop both loops together using exit button.

P.S:1.My statemachine is inside a while loop and a while loop is running inside my sub vi also.

        2.Both the while loops are running parallel since i am using queues in it.

 

0 Kudos
Message 1 of 7
(3,284 Views)
Solution
Accepted by topic author qwerty7

        2.Both the while loops are running parallel since i am using queues in it.


There are numerous ways, depending on your architecture...

 

Can you pass an exit message from your event loop to your other loop on the queue?

 

Another way is to use a 'Stop' global boolean variable and use this in the exit condition of your second loop. Set it in the event loop when you wish to stop the program.

 

The producer consumer example stops the second loop by closing the queue when the first loop stops, and then capturing the error when the second loop tries to read the queue.

0 Kudos
Message 2 of 7
(3,263 Views)
Solution
Accepted by topic author qwerty7

@qwerty7 wrote:  2.Both the while loops are running parallel since i am using queues in it.

There is your solution.  You just need to send a stop command through the queue.  What is your current data type for the queue?  I have some tricks depending on the data type.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 7
(3,250 Views)

Thanks using global variables helped but while using global variables wouldnt it lead to race conditons. 

0 Kudos
Message 4 of 7
(3,210 Views)

The use of global variables is generally frowned upon because they can so easily lead to race conditions which are difficult to spot and debug. However, it's the way you use them, not just the fact of using them, which results in race conditions.

 

There are some who swear blind they should never be used. Personally I think they have their uses, but they are limited, and I would probably avoid using them for this purpose if possible (despite suggesting it in my original answer!)

 

That said, there are usually more robust ways of achieving the same thing - for instance using the queue in the example you gave.

 

NI have a brief tutorial on stopping parallel loops: Stopping Parallel While Loops in LabVIEW With One Stop Button

 

0 Kudos
Message 5 of 7
(3,199 Views)

@qwerty7 wrote:

Thanks using global variables helped but while using global variables wouldnt it lead to race conditons. 


Possibly.  If you have multiple loops writing to the variable.  Generally, I like to use global variables for "constants" (values that never change or rarely).  You could consider the stop global to fit that definition.  But the issue you could possibly have here is that you need to check that variable every so often.  For your loop with the Dequeue, that means you will have to have a timeout and handle that timeout.  This kind of defeats the point of having a queue that sleeps while waiting for data to show up.  This is one major reason I send the stop commands through the queue.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 7
(3,174 Views)

An alternative approach I ften use is to register a dynamic event that is triggered when the top lvel goes idle.

 

In post #18 of this thread, I posted a zip that uses that technique to close sub-VIs.

 

If you have not used dynamic events, then look at Ton's nugget for an introduction.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 7 of 7
(3,141 Views)