LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

taking action only the first time an event executes

I want a certain action to occur only the first time an event handler for a button is called. Is there a simple way to do this?

Bill F
0 Kudos
Message 1 of 6
(3,312 Views)
There are two pretty easy ways to do this.
If you want to continue to trap other events (other event cases in your event structure), use a shift register:
1. Add a shift register to the loop containing your event structure. (right-click on the left-hand border of your loop and select Add Shift Register).
2. Wire a numeric constant = 0 from outside the loop to the input (left-hand) terminal of your shift register.
3. Inside your event structure, in the event case you want to handle only once, put a case structure around the code you want to execute only once. That code should be within the True case.
4. Add an Equal To 0? function (from the comparison palette) to the loop. Wire the input shift register to the input of Equal To 0?. Wire the output of Equal To 0? t
o the selctor for your new case.
5. Within the True case of your new case structure, add a numeric constant = 1. Wire that constant out to the output (right-hand) side of your shift register.
6. Within the False case of your new case structure, wire the input side of your shift register to the output tunnel created by the wiring in step 5.
This way the event structure will keep running and handling other events, but the case you just modified will only run the first time: the first time you get the event, the shift register will = 0 so you'll execute the True case of your new case structure. Within that case, you set the shift register to 1. The next time you get that event, the shift register = 1, not 0, so you'll execute the False case and not the code in your True case.
If you want to stop all event handling after the first event and your event handler is the only thing with its loop, from the event case (for the event you want to stop on), wire a True boolean constant out to
the Stop terminal of the loop containing your event structure.
0 Kudos
Message 2 of 6
(3,312 Views)
Thanks for a detailed answer. I had tried that before and it didnt work - value stayed 0 so one-time code executed every time. I realized that I had to wire the shift register through on all the other event handlers as well. It was using the default - which is 0
0 Kudos
Message 3 of 6
(3,312 Views)
The method that AlS gave will work. It appears that you are either not changing the value to 1 inside the event structure or you are inadvertently changing the value in other events in the structure. A copy of your code would be helpful in analyzing where the problem is.
0 Kudos
Message 4 of 6
(3,312 Views)
Sorry, I didn't mean to imply it didn't work. It does. What I tried to say was that I originally tried using a shift register and it didn't work. The problem is that if you have multiple events - you must wire the shift register thru every event - not just the one containing the code you only want to run once. If you don't and another event executes, you get the default value into the shift register - which is zero
0 Kudos
Message 5 of 6
(3,312 Views)
That is correct. The data flow must be continuous for every possible event.
0 Kudos
Message 6 of 6
(3,312 Views)