LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Why does while loop continue 1 more time after stop command is true?

I have a while loop that runs fine until I set the "stop if true" conditional to true. My stop message appears, I click my "OK" button,
and I expect the vi to stop running the while loop.  It does not.  It performs the while loop 1 more time, the stop message appears, i click ok again
and the while loop exits.  I have "seen" the condtional to be true with a probe and highlight execution at the correct time for stopping the
execution of the while loop. 

thanks all





0 Kudos
Message 1 of 10
(4,698 Views)
can you post a portion of your code or all of your code so that we can look at it.

I tried making a While loop with a stop dialog to see if I got the same "error" that you get, but I could not reproduce it.  Once I hit the stop button, it brought up a dialog to ask if I really wanted to quit, and if I said yes, then it stopped, no more loop counts.

Kenny
Kenny

0 Kudos
Message 2 of 10
(4,684 Views)

As Kenny said it is very difficult to say without seeing the block diagram.  Just a guess though, I have seen cases where the stop button is read while the loop continues; you press the stop button and it is not read again until the next iteration.  Check your data flow to see when the stop button is read.

Steve

Message 3 of 10
(4,680 Views)

Hi Jonusmc,

      We all fall victim to this at least once.  So you have some test for whether to loop or not - naturally this happens last, right?  Well in text based languages it happens last if you put it last, but in LabVIEW it happens as soon as it possibly can - based on data-dependency.  The "flat sequence" is specifically designed to force code segments to execute sequentially, but it's often enough just to put a single frame around some section of code, then attach a wire to it so it has to wait before executing.

Maybe I'm way-off with this answer, but it sounds so familier! Smiley Wink

Cheers

 

When they give imbeciles handicap-parking, I won't have so far to walk!
Message 4 of 10
(4,658 Views)
I agree with dynamik,

What's happening is that your code is reading the button at the beginning of the loop (FALSE), and some time while your loop code is executing, the button changes to TRUE.  However, since the button is only read once per loop, it'll only be caught on the next iteration.

If you have different points reading the button, it could well be that one of them gets the TRUE because it reads the button at a later stage, whereas the other has the FALSE value read out previously.  This is a classic race condition.

You should only read the button at a single place and wire it through any structures you need.  Or even better, only read the button at the end of your code execution, this will achieve what you're looking for (Sequence structures can force execution order as Dynamik already pointed out).

Hope this helps,

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Message 5 of 10
(4,656 Views)
To all how answered - my thanks

The read at anytime makes sense - now I will go back the the code with that in mind to try and fix the problem -

Again thanks fro jonusmc

0 Kudos
Message 6 of 10
(4,636 Views)

I have found a great solution in this regard.

Put all of the codes in the while loop in a case structure.

If you press the stop button, the while loop will not continue 1 more time.

Ashkan8203_0-1601971129718.png

 

0 Kudos
Message 7 of 10
(2,915 Views)

Hi Ashkan,

 


@Ashkan8203 wrote:

I have found a great solution in this regard.


Well, a "great solution" including a Rube-Goldberg!

Why do you need that NOT function? Simply switch TRUE/FALSE cases…

 


@Ashkan8203 wrote:

If you press the stop button, the while loop will not continue 1 more time.


This sentence is wrong: the loop will iterate as often as without this case structure!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 10
(2,907 Views)

**Why do you need that NOT function? Simply switch TRUE/FALSE cases…

 

Because the question is about using a stop button!

 

**This sentence is wrong: the loop will iterate as often as without this case structure!

You are right. But, I told that the whole code must be in the case structure. 

0 Kudos
Message 9 of 10
(2,901 Views)

Hi Ashkan,

 

when you are answering all those very old threads then you should supply good solutions!

 

This would be a "great" solution IMHO:

The first sequence frame is just for illustration, the main point is to use THINK DATAFLOW to read the stop button just at the end of the code inside of the loop!

 


@Ashkan8203 wrote:

**Why do you need that NOT function? Simply switch TRUE/FALSE cases…

Because the question is about using a stop button!


But this NOT function still is Rube-Goldberg as you can switch cases as easily as the stop condition of the loop…

And the question is not "about using a stop button", but about "when to read the stop button"! (It's a kind of race condition…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 10
(2,896 Views)