LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

To use or not to use: while loop?

Solved!
Go to solution

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.

0 Kudos
Message 1 of 8
(3,195 Views)
I'll give you a great example when to use a While Loop: wrapped around your Event Structure. 🙂 Are you referring to when you would use polling versus an Event Structure?
Message 2 of 8
(3,191 Views)
Solution
Accepted by topic author murchak

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.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 3 of 8
(3,187 Views)

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!

0 Kudos
Message 4 of 8
(3,175 Views)

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.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019
0 Kudos
Message 5 of 8
(3,145 Views)

@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

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 6 of 8
(3,135 Views)

@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. Smiley Wink



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 7 of 8
(3,122 Views)

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.

 

Message 8 of 8
(3,109 Views)