I am experiencing A strange behavior while trying to stop a loop.
I have A loop which should stop either conditionally or by pressing the STOP button.
The stop button resides in a event structure since the code is preforming some nested loop.
When I place the Stop Button in the event structure. the loop does not stop even though a "TRUE" was conditionally.
What am I doing wrong?
I have attached the VI.
I cannot open your vi. Can you save it for a previous version?
The event structure waits for an event. That's what it does.
So it will wait, and it won't stop until you press the button.
If you want it to, you can wire a time out and add a time out case to the event structure. That way, the time out will be triggered, and the loop will not be kept by a waiting event structure.
It's silly of source, because you might as well simply skip the event structure!
Event driven is a better way to do things, but if you tweak the event structure like that, you're almost simply polling the value. Almost, because when polling you could miss the button status if a user clicks it twice. This will trigger the event structure (twice if you'd let it). So it is a little bit better then polling.
If you want to use event structures, you can put all 'idle' code in the time out case of the event structure. The main while loop will have only the event structure in it, and the event structure will have all idle stuff in the TO, and the stop event will stop the loop.
There's a danger in that, because if you add for instance a mouse move event, the time out will never be triggered when you move the mouse. That's why people tend to make (at least) two loops. For instance in a producer/consumer pattern.
Also, the loops stops on an error, and has nothing to do with the stop button.
The error won't be 2 the first iteration, so when you press the button, the error code was already evaluated (code=0), stop is false, loop continues to next iteration, loop waits for new event...
Also, use end termination characters for your serial communication. Bytes at port is a terrible way to do it. End termination characters will eliminate the need for the wait, so the communication can run as fast as it can. It will also eliminate the need for that dreadful sequence structure.
Hi, Thanks for your response but still do not understand...
If I do not use the event structure, the loop will not instantly stop when I press the stop button.
If I use the the event structure, the loop will stop instantly as I press the stop button, but will not stop on error code 1 or 2.
I am at a loss here.
I did not send the sub VI. both of them have loops which also stop with the variable stopAll
Can you please help me with solving this enigma?
Use the logical OR of all three conditions (error 1=TRUE OR error 2=true OR stop button=true) to update the shared variable and write to the loop termination condition.
(remove the event structure because all it does is prevent the loop from spinning.)
And Again thanks for the reply.
I used OR as you suggested. but since the subVi are running loops waiting for connection. the program will not stop until the subVI loops have existed.
The subVi loops are also connected to the stopAll shared variable. but it does not have an effect while the inner loops are looping.
I have attached one of the subVi.
You didn't attach your subVI's so we can't see exactly what you did in them.
But in general, subVI's shouldn't have while loops inside of them. Them them do their thing and return back to the Main VI. If they need to do something repeatedly, have the while loop in the main VI repeatedly call the subVI.
If you do need the subVI to have the while loop, then have a mechanism such as a queue that will allow the maing VI to let the subVI know when to stop.