LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

stopping while loop not working properly

HI,

 

I'm trying to stop a while loop through queue operation in an event structure in another while loop. It turned out the stop button is not immediately stop the while loop after pressed. Any recommedations?

 

p.s : I cannot give the original vi but attached vi represents my problem.

0 Kudos
Message 1 of 5
(3,832 Views)

What is the difference between Stop and Process Stop?

 

Create an event for your Stop button as well.  Right now you press stop, but you are pretty much going to need to trigger two other events in that event structure (unless you add a timeout case) in order to get the stop button to be read as True and all code inside to execute in order for the current iteration to end, and the next iteration where the Stop button is finally read as true.

 

Execute your code with Highlight Execution on so you understand what is happening.

0 Kudos
Message 2 of 5
(3,816 Views)

There are several options to do this, and a couple of problems with your method.

 

The main problem is that in your "consumer" loop you have a dequeue element.  This means the loop doesn't "run" as much as it iterates once every time a message is sent across the queue.

 

I would suggest starting by looking at the queued message handler example.

0 Kudos
Message 3 of 5
(3,812 Views)

Your Top While Loop didn't stop because it waits for an event in the event structure because timeout is -1.

If you push the Process Stop Button the event case flushs the queue but the botton loop waits for an queue element because the queue timeout is -1.

 

- Add an event case for timeout and set the timeouttime from the event case to 100 ms. Then the top while loop checks every 100 ms the state of the stop button. (add event case - Application - Timeout)

- set the timeout of the dequeue element function in the botten case to 100ms an check the timeout? terminal, is it true than ignore the data (case structure).

- to stop the botton loop i would destroy the queue (release queue function) in the top loop at the Process Stop Button and check the error out of the dequeue function in the botton queue, is there an error than stop the bottom loop (not the best method but it works for simple vi's)

0 Kudos
Message 4 of 5
(3,806 Views)

And while Bill is correct, You have a classic mistake in that code.

 

Lets look at the Stop Process Value change event.

 

When the button is pressed you flush the queue removing anything in it.  you don't force destroy the queue, you don't enqueue a "Quit" command you simply empty the queue.

 

What effect does this have on the consumer loop?  Well, the dequeue element will be waiting forever to receive an element.  Its happy to wait forever.  it will patiently wait untill the PC rusts into nothingness and fails.  It will still be waiting when the seventh trumpet is sounded, or the sun goes supernova or ... Whatever the end case is.  The consumer loop will not iterate without anything pushed into the queue or the queue is destroyed.  Since the loop can't iterate it can't stop.  


"Should be" isn't "Is" -Jay
0 Kudos
Message 5 of 5
(3,801 Views)