LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

When to use events

Hi, I have been trying to teach myself LabVIEW for a few months now.
I have learned a lot, most of which I learn as I need it.

 

I have seen a lot of people mention "events", so I have been trying to learn about those.
It appears to me that events are chunks of code that run when a user does something (ie clicks a mouse or presses the keyboard).

 

What I don't understand is, why is this different than wiring a button on the front panel to control some loop (while loop, for loop, etc)?

When would you use one instead of the other?

Cory K
0 Kudos
Message 1 of 5
(2,883 Views)

When you use a while loop and read the status of a front panel Boolean, you are using what is called polling. So, depending on what kind of wait you have in the loop, the loop is spinning constantly. When you use the event structure, the loop does not spin. It just waits for an event to fire. It's much more efficient in terms of cpu usage. It makes for a 'neater' diagram since you don't have to create separate case statements for each Boolean. If you wanted to monitor a numeric control for a change, without the event structure you would need a shift register to keep the old value and then do a comparison to the current value - more polling and additional wiring.

 

There are other reasons. I think the event structure is one of the greatest additions to LabVIEW. It made the creation of user interfaces so much simpler.

Message 2 of 5
(2,873 Views)

In this nugget I describe the differences between the two modes

  1. Polling (like you do) uses a lot of CPU cycles, if you have a lot of buttons you have to check a lot of things
  2. Event driven uses less CPU cycles, code only executes when it is needed.
Ton

 

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 3 of 5
(2,871 Views)

Ahhhh, that makes a lot of sense.

OK, well then I have another question

 

It is easier if I give you an example:

Say I have an event that runs some code when i press "k".

Will the code only run the first time i press "k"? or will it run everytime I press "k"?

 

Also, when you were talking about "polling" does a case structure do that too? or just a while loop?

 

 

Cory K
0 Kudos
Message 4 of 5
(2,869 Views)

In general a loop (while or for) without an event structure are polling loops, they are controlled by timing or CPU power. (for every rule there are exceptions but we ignore them).

 

In your case you will need an event structure inside a while loop. This way the while loop only spins if the event is triggered.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
Message 5 of 5
(2,866 Views)