04-16-2014 09:10 AM
Hi there,
I have a while loop to run some sequence tasks every some minutes and will stop until user press STOP button.
In this example, TASK1 and TASK2 will be executed every 100000ms. But I find that if user press STOP during the last frame (wait) it won't response to the stop and quick the application. Any way to accomplish this stop and make it of highest priority? Thanks.
Solved! Go to Solution.
04-16-2014 09:30 AM - edited 04-16-2014 09:32 AM
Instead of a hundred second frame, make it a while loop. Have the while loop run at about once per second and only stop when the Elapsed Time Express VI reaches 100 seconds OR the stop button (now placed inside of that while loop) stops.
The stop button will also wire out of the inner loop to stop the outer while loop.
Another possibility is to use an event structure in that frame with a 100 second timeout case, and a case for the Stop button's Value Change event.
04-16-2014 10:26 AM - edited 04-16-2014 10:54 AM
use a simple state machine....sequence of events are TASK1>TASK2>EXIT. your software timing should be ALWAYS done with timers and not the "wait ms" loop iteration as the front panel can only responds as quickly as it is set.
04-16-2014 10:34 AM
You could also use an event structure with a very long timeout and define value changed event of the necessary buttons. You can recalculate the timeout whenever another event occurs so the timeout occurs in globally regular intervals.
I posted some code long ago, I'll find it later.
04-16-2014 10:41 AM
@RavensFan wrote:
Instead of a hundred second frame, make it a while loop. Have the while loop run at about once per second and only stop when the Elapsed Time Express VI reaches 100 seconds OR the stop button (now placed inside of that while loop) stops.
I'd actually make it a FOR loop with the conditional terminal turned on. Set N to 100 and wire the stop button the the conditional terminal.
Honestly, I think the best solution is to use a state machine. In the state machine you have Task1, Task2, Idle (used to check for the button presses), and Check Timeout. In the Check Timeout you can use the Elapsed Time function to see if your have reached your 100 seconds and proceed as necessary.
04-16-2014 10:58 AM
Here is an old example of my suggested solution using event timeouts. (discussed here)
04-16-2014 11:56 AM - edited 04-16-2014 11:56 AM
@altenbach wrote:
You could also use an event structure with a very long timeout and define value changed event of the necessary buttons. You can recalculate the timeout whenever another event occurs so the timeout occurs in globally regular intervals.
I posted some code long ago, I'll find it later.
Hi altenbach,
Thanks for your reply. I am reading your code now. I have a question. It seems that you are manipulating the timeout (to make it down to zero when the stop button pressed. So I think I should put the task to the timeout event frame, do I? If I press stop button while task1 and task2 are still in execution, it seems that your code frame will wait both tasks to be done before quite the while loop, is that correct? That's exactly what I am looking for if that's the case.
04-16-2014 12:07 PM - edited 04-16-2014 12:14 PM
No, the stop button stops the unconditionally and immediately unless the timeout event is execution at the time stop is pressed.
If the timeout event is execution at the time, then the timeout even needs to complete, of course before the VI can stop.
04-16-2014 12:08 PM - edited 04-16-2014 12:09 PM
Do Task 1 and Task 2 take a long time to execute? The way you have your example drawn now, it doesn't seem so since you've put the long wait state in only the 3rd frame.
Altenbach isn't trying to duplicate your example exactly. (He can't because he wrote that example 9 years before you asked your question.) He's just showing you method to work with and shorten timeouts that may give you ideas. What is special about his is that if you have an event that doesn't stop the VI, it allows the code in the timeout case to execute when it was originally scheduled to execute rather than have the timeout timer reset back to the full value.
04-16-2014 12:26 PM
@RavensFan wrote:
Do Task 1 and Task 2 take a long time to execute? The way you have your example drawn now, it doesn't seem so since you've put the long wait state in only the 3rd frame.
Altenbach isn't trying to duplicate your example exactly. (He can't because he wrote that example 9 years before you asked your question.) He's just showing you method to work with and shorten timeouts that may give you ideas. What is special about his is that if you have an event that doesn't stop the VI, it allows the code in the timeout case to execute when it was originally scheduled to execute rather than have the timeout timer reset back to the full value.
I got the point now. But Task1 and Task2 could be short or long (it is actually data collection) sometimes it could take more than 1 minute to run. I tried to modify Altenbach's example and make the loop (in timeout frame) so long and I see that the stop button won't response until the the task done.