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: 

how to make the button response in long wait loop?

Solved!
Go to solution

Hi there,

  I have a while loop to run some sequence tasks every some minutes and will stop until user press STOP button.

 

a.png

 

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.

0 Kudos
Message 1 of 12
(4,057 Views)

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.

0 Kudos
Message 2 of 12
(4,050 Views)

Example_VI_BD.png

 

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.

0 Kudos
Message 3 of 12
(4,029 Views)

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.

0 Kudos
Message 4 of 12
(4,024 Views)

@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.


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 5 of 12
(4,016 Views)

Here is an old example of my suggested solution using event timeouts. (discussed  here)

0 Kudos
Message 6 of 12
(3,998 Views)

@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.

0 Kudos
Message 7 of 12
(3,982 Views)

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.

0 Kudos
Message 8 of 12
(3,975 Views)

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.

0 Kudos
Message 9 of 12
(3,973 Views)

@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.

0 Kudos
Message 10 of 12
(3,964 Views)