06-17-2015 02:48 AM
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.
Solved! Go to Solution.
06-17-2015 04:46 AM
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.
06-17-2015 05:37 AM
@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.
06-19-2015 12:16 AM
Thanks using global variables helped but while using global variables wouldnt it lead to race conditons.
06-19-2015 12:38 AM
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
06-19-2015 05:43 AM
@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.
06-19-2015 10:18 AM
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