05-11-2012 10:49 AM
Being relatively new to LabVIEW, and excited about Event Structure programming makes me think that a While Loop should almost never be used.
A While Loop uses "polling" to see whether a condition is met or not before stopping execution, which eats up memory. While (no pun intended) Event Structures
don't constantly gobble up the CPU memory and wait until something happens, then executes.
Can someone give me a good and simple example of when it is proper to use a While Loop? Feel free to talk about some more complicated situations that require a While Loop
that really cannot be address with Event Structure programming.
Solved! Go to Solution.
05-11-2012 10:57 AM
05-11-2012 10:58 AM
While loops and event structures go hand in hand. You almost always want to place the event structure inside of a while loop. If you don't you will service one and only one event. The while loop will allow you to service all events. It is important to note that when there are no events the while loop will not be doing anything. It will be idle and consume essentially no CPU time. The event structure will force it to wait for the next event.
05-11-2012 11:07 AM
Thank you both Darin and Mark.
So when I place the event structure inside the while loop, the while loop does NOT use CPU memory when the "event" controlled by the event structure hasn't happened?
Wow, I now feel quite silly for not thinking that. It really does make a lot of sense to place a event structure inside a while loop! Thanks guys!
05-11-2012 02:46 PM
You've got it but there is an exception. The event structure has a timeout event included by default. If you don't wire the timeout terminal at the top left of the stucture (or wire a value of -1) it will never time out and the while loop around the event structure will not spin. BUT, if you do wire a millisecond value (X) to the terminal the event structure will allow the while loop to spin every X number of milliseconds. This can be very useful if you want to avoid wasted CPU cycles but you still want some activity in the loop. A watchdog timer or an elapsed timer that only updates every 10 second for instance.
05-11-2012 03:02 PM
@Mark_Yedinak wrote:
While loops and event structures go hand in hand. You almost always want to place the event structure inside of a while loop. If you don't you will service one and only one event. The while loop will allow you to service all events. It is important to note that when there are no events the while loop will not be doing anything. It will be idle and consume essentially no CPU time. The event structure will force it to wait for the next event.
"almost always"
There is a situation where I do not use the while loop. Pop-up dialogs. I let the user do what hey want with the controls on the FP but only read them afte the event for the "OK" button fires.
Asie from that my events are inside while loops.
Ben
05-11-2012 03:49 PM
@Ben wrote:
@Mark_Yedinak wrote:
While loops and event structures go hand in hand. You almost always want to place the event structure inside of a while loop. If you don't you will service one and only one event. The while loop will allow you to service all events. It is important to note that when there are no events the while loop will not be doing anything. It will be idle and consume essentially no CPU time. The event structure will force it to wait for the next event.
"almost always"
There is a situation where I do not use the while loop. Pop-up dialogs. I let the user do what hey want with the controls on the FP but only read them afte the event for the "OK" button fires.
Asie from that my events are inside while loops.
Ben
Which is exactly why I didn't say always.
05-11-2012 05:59 PM
murchak wrote:Can someone give me a good and simple example of when it is proper to use a While Loop?
A while loop is required for repetetive tasks where the number of iteration cannot be known when the loop starts. Since this is true for virtually any program, you will not be able to find any professionally written toplevel LabVIEW program that does not have a while loop as the most important core structure of the toplevel diagram. (remember, "run continuously" is a debug tool, not a sane mode to run code).
Since the function of a while loop and an event structure is completely different, one cannot substitute for the other. As other have said, they can complement each other for the code sections that deal with user iteractions.