09-21-2018 10:50 AM
Hi
The attached VI does not stop after the VI reads the Stop. It stops after finishing the next loop. Why? I wish to stop the loop right away after the Stop button is pressed or at least after the VI reads the Stop button. How can I do that?
Thank you,
Steve
Solved! Go to Solution.
09-21-2018 10:59 AM
If I slightly re-arrange your diagram like this...
Does that make the problem more apparent?
How about like this?
If you couldn't guess what I'm getting at, LabVIEW will parallelize any code that it can. In this case, there is no data dependency between the case structure and when the stop button gets read.
Odds are, the VI is already waiting when you press the stop button - but the stop button has already been read in that iteration of the overall while loop. There are a couple ways to solve this:
1. Put the reading of the 'stop' button after the case structure executes using data dependency (aka sequence structure)
Cons: If the code in the while loop takes a long time to execute, it could appear that the stop button isn't responding to UI events as it will take a few seconds for LabVIEW to read the state of the button.
2. Have an event structure in the while loop and make the state machine check for events or wait at the event structure when idle (look up the State Machine sample projects or the JKI State Machine good examples of this)
3. If this will eventually turn into a larger application, consider looking at the Queued Message Handler, where you can completely de-couple your UI handling with the processing
-Nate
09-21-2018 11:00 AM
This VI is a simplified case for my application. I need to use the State Machine structure. Any help would be appreciated.
Steve
09-21-2018 11:08 AM
09-21-2018 11:09 AM - edited 09-21-2018 11:10 AM
Nathan already answered your question. You need to make sure the last thing you do in the loop is read the value of the stop button. As your code is currently written the value of the stop button is one of the first things read. So after you complete whateve code is going on in your case structure the code will go to the next iteration because the stop has already been read and it is false. Force the read of the stop button to occur after the case structure occurs.
However in a true state machine I would have state action which specifically checks for the stop condition rather than stopping it in parallel. This way you can invoke any states that are required to shut everything down gracefully and then exit. I would also have a Exit case which is the only way out of the state machine.
09-21-2018 11:21 AM
I usually have a "Check For Events" or an "Idle" state where I have an Event Structure in order to check for button presses, updated values, etc. And 100% agree with Mark, you should then run specific states to shut everything down properly and then stop the loop.
09-21-2018 11:22 AM
Hi Nathan and Mark,
Thank you very much for your help. The attached VI basically solved my problem. Now I am wondering whether there are any ways to stop the loop right away after the Stop button is pressed. Maybe I am asking too much.
Regards,
Steve
09-21-2018 11:27 AM
My real application has the Exit case. The attached VI is just a simplified case. I just want to illustrate the Stop process.
Thank you
09-21-2018 11:29 AM
Not really. At least not easily. Remember, within a loop ALL code inside it must run to completion. This means that your for loop must complete before you stop. You would need a much more comprehensive architecture which would allow you to break out of long programming tasks. This is non-trivial and usually unnecessary. If you use a producer consumer patter you can catch the the user wants to stop and provide them with feedback that you will be stopping but some code needs to complete execution. That would be about the easiest way to give immediate feedback to the user that you will be stopping.
09-21-2018 11:38 AM
Hi Mark,
Thank you very much. I'll try that.
Regards,
Steve