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: 

Force the While Loop to exit when Delay is executing

Solved!
Go to solution

Hello guys,

 

I have a While Loop contaning some jobs. One of them is delay for 10 seconds. When I push the stop button, the loop doesn't stop immediately. Instead, it will stop after a few seconds (may be, waiting the other jobs is done).

 

What can I do if I want to stop the loop immediately after I press the stop button?

 

Thank's for sharing..

 

 

0 Kudos
Message 1 of 12
(17,463 Views)

hi

the loop will stop after the Stop button is read in the cycle, so, because you have a wait funtion of 10 sec, sometimes you may need to wait 10 seconds before stop. What you need to do is :

make a shift register, initialize with actual time (Now in the time palette)

put a wait for 250 msec or so (make sure you wire the error cluster)

compare the actual value of time with the one stored in the shift register (if samller than 10 sec do nothing, otherwise  make what do you need to do AND change the time value of the shift register...)

in this case, the maximum wait time will be 250 milliseconds

(of course there are other ways : see the Time functions), but this one is a simple one

N

 

 

0 Kudos
Message 2 of 12
(17,451 Views)
Solution
Accepted by topic author yoppy

Hello Yoppy,

 

I am not sure if I have understood your problem completely, but below is one of the method that I use to stop the while loop immediately (Some ms delay will be there for sure).

 

Let me know if the attachment helps.

 

There are many ways to do it and you may get better solutions than this one on this community.

Message 3 of 12
(17,449 Views)

as a simple solution use the "Elapsed time" in the timing pallete.

 

Wire the "Reset" input with your Stop button and it will work for you as shown

 

1.png

Regards
Guru (CLA)
Message 4 of 12
(17,439 Views)

Hi yoppy, this is Paul with Applications Engineering at NI.

 

What others have said about the stop button is true because of the way LabVIEW handles data flow.

 

The best way to ensure stoppage of your program is to use an event structure, which may be a bit much for your simple program, but still check out these tutorials:

 

http://zone.ni.com/reference/en-XX/help/371361G-01/glang/event_structure/

 

http://zone.ni.com/devzone/cda/tut/p/id/12293

 

Also, unrelated to this, I see you are not handling errors properly here.  By placing the error handler within your while loop, you will create a loop of errors that can cause LabVIEW to hang.  I didn't full recreate your code, but it should look something more like this:

 

error handling.png

 

In this code, an error will stop the while loop via the unbundle function, and then will handle the error outside of the while loop.

 

For more info, check out: http://www.ni.com/gettingstarted/labviewbasics/handlingerrors.htm

Paul Davidson
National Instruments
Product Owner - ni.com Chat
0 Kudos
Message 5 of 12
(17,387 Views)

Thank you for all your solutions guys. I feel that the solution from Krunal Patel suits my purpose the most. Still I have one question. Please see attachment below. Based on dataflow concept, I think that the duration for each one loop would be 6 seconds.

 

I think the process goes like this. The While Loop will loop until 5 seconds. Then, the case structure becomes TRUE and executes codes inside. These codes execution will take about 1,... seconds (write VISA -> delay 1 second -> read VISA). It is one cycle. After that, the loop re-iterates. So, each one loop will take 5 + 1  = 6 seconds.

 

But when run this code, I get different result. The duration for one loop is 5 seconds. It seems that when the TRUE Case sturucture is executing, the Get Date/Time is also executing simultaneously.

 

What I know about dataflow concept is that the TRUE Case structure should be done first. After that,the loop can re-iterates.


0 Kudos
Message 6 of 12
(17,325 Views)

Your first time through, you probably get 6 seconds, but every time after that will be 5.

 

When you hit your 5 seconds mark, that time from the Get Date/Time which gets read at the beginning of your loop is placed into the shift register.  Then your True case runs and takes a second.  At the beginning of your next loop iteration, you take the time stamp again, but 1 second has already passed compared to the time stamp in the shift register because thats how long the previous loop iteration took when you ran the True case.

Message 7 of 12
(17,322 Views)

Yes. Thank you Ravens. Why do I never think about that.. Smiley Tongue It takes 5 seconds even for the very first iteration. It is the time stamp (TIME has not ever stopped since the beginning of TIME Smiley Happy) that makes the While loop seems to never wait for the TRUE Case. Many many thanks.

0 Kudos
Message 8 of 12
(17,318 Views)

I think you could also use a STOP vi from the application control palette.

i used to put this vi on every single while loop and it worked just fine.

i'm not sure if its the ideal thing to do but it's done the job.

 

 

0 Kudos
Message 9 of 12
(11,480 Views)

It's FAR from the ideal thing.  If you want a graceful shutdown STAY AWAY FROM THE STOP.VI.  Using the stop.vi is like using the abort button.  It will stop LabVIEW in its tracks.  Any references you have opened will remain open.  Any equipment that you are using will be in an unknown state.

 

If you want to eject the warp core, then the stop.vi is for you.  If you're interested in shutting down in an orderly fashion, then use one of the above methods.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 10 of 12
(11,474 Views)