VI High 8: More on How to Program Events with the Event Structure in LabVIEW

In this continuation of our last episode, we examine one of the most commonly asked LabVIEW programming questions: how do I use the event structure? We examine executing two events from the same event case and the basics of programming a timeout event case.

For more on learning LabVIEW, check out the Sixclear Lucid LabVIEW Fundamentals Training (formerly Sixclear LabVIEW Fundamentals) course offered by Sixclear: sixclear.com.


In the video, we mention that we don’t need to place a numeric terminal in an event case tied to that numeric but we must place the terminal of a latch action Boolean in the event structure case corresponding to that Boolean. Why is that? This is taken from the LabVIEW Help topic ” Using Events with Latched Boolean Controls”:
“When you trigger an event on a Boolean control configured with a latching mechanical action, the Boolean control does not reset to its default value until the block diagram reads the terminal on the Boolean control. You must read the terminal inside the event case for the mechanical action to work correctly. As a reminder, a note appears in the Edit Events dialog box when you configure a Value Change event on a latched Boolean control.”

Experience Level: Basic

(start transcription)

“Last week, we showed how to create a very simple event-driven program by programming an event structure to wait on a value change event on a front panel OK button. At the end I left you on a cliff, waiting for this week to learn how to program multiple events as well as explore other caveats. Are you ready?
First, it’s important to point out that the event structure is like a case structure, stacked like a deck of cards, so that you don’t see an underlying case when you’re looking at the top. This event case is waiting on the OK button value change, and this one is waiting on the stop button value change. Let’s say now that I would like to have 2 events generated from this same event case. For instance, I’ll place this numeric control on the front panel, and I would like the same dialog box to pop up if I pass my mouse over this numeric or if I click on the OK button as before. First, I’ll change the dialog box text. Then right-click on the border of the event structure, choose Edit Events Handled by This Case, and in the Edit Events dialog box, I’ll click Add Event. Then in the Event Sources window I’ll choose my Numeric, and in the Events window I’ll expand on the Mouse category and choose Mouse Enter, which is when my mouse enters the bounds of this Numeric control. I see both of these events configured and I’ll click OK. Now if I create some space in this event case by holding down the control key on my keyboard and click and drag my mouse to the right, expand the window, I see both of my events listed in the selector label.
Let’s run my VI. As before, if I I click on the OK button I get the dialog box, and passing my mouse over the Numeric gives me the same event. Note that I did not need to place the Numeric terminal inside of the event case as I needed to with the latch action booleans. For the reason, see the notes in the text description of this video.
Now what’s this hourglass up here? It’s the timeout terminal, and it specifies how long we’ll wait before we execute the timeout case. Currently, we don’t have a timeout case, though it is the default type of event if we were to just place down a new event structure. But let’s configure a timeout case. Right click on the border, add an event case, I’ll choose Application as the Event Source—in other words events that would be generated from the LabVIEW environment—and then choose Timeout as the Event. Ok, and now I’ll put something in this event case, we like dialog boxes, there we go. Now how long does the event structure wait before executing this case? Let’s create a constant off this terminal and see that the default is -1, which means forever. But we don’t want to wait forever, we’ll instead enter 3000 or 3000 milliseconds, which is 3 seconds.
So now I’ll run this again, and just as before, we have events generated from our other controls, but if I don’t generate any event for three seconds…I get the timeout event. So pushy.
Well that’s it for this week, there are many other things I can do, like have more than one event tied to the same object, create filter vs. notify events, and even create programmatic events such as user events. If you’re interested in some of those things, check out the notes for this video or sixclear.com.”

(end transcription)

VI High 7: How to Program Events with the Event Structure in LabVIEW

This time around, we examine one of the most commonly asked LabVIEW programming questions: how do I program with events? We’ll explore why this is different than the traditional “polling scheme” and basics of the event structure. We’ll talk about this more in a future episode.

For more on learning LabVIEW, check out the Sixclear Lucid LabVIEW Fundamentals Training (formerly Sixclear LabVIEW Fundamentals) course offered by Sixclear: sixclear.com.


In the video, we mention that we must place the terminal of latch action Booleans in the event structure case corresponding to that event. Why is that? This is taken from the LabVIEW Help topic ” Using Events with Latched Boolean Controls”:
“When you trigger an event on a Boolean control configured with a latching mechanical action, the Boolean control does not reset to its default value until the block diagram reads the terminal on the Boolean control. You must read the terminal inside the event case for the mechanical action to work correctly. As a reminder, a note appears in the Edit Events dialog box when you configure a Value Change event on a latched Boolean control.”

Experience Level: Basic

(start transcription)

“I like an eventful life, so when Friday night rolls around, I program my own. You may have other weekend plans, so I’ll teach you how to do it in in five minutes. First, what are events? An asynchronous notification that something has occurred, the most common thing to have occurred is some action on our front panel. Let’s say when I click on this OK button, I want a dialog box to pop up saying: “You clicked the button”. First let’s do this without events. We’ll look at my block diagram. You see I’m wiring the OK button to the case selector terminal of this case structure enclosed by a while loop. And in the true case of the case structure, I have the one-button dialog function. It’s also a good idea to put some timing in here to dictate the frequency of loop execution: every 10 ms should be fine.
Going back to my front panel, let’s run this VI. When I click on the OK button, the dialog box pops up. Looks good. We call this type of design a polling scheme, because LabVIEW is continuously checking or polling this OK button to see if it’s been pressed. How often? 100 times per second. Let’s highlight execution this. Indeed, we see that over and over again, the OK button is polled and reads false, meaning that we go to the false case where there is nothing. Once the OK button has been pressed, we go to the true case and execute the dialog box function.
This works, but is there a better way? Yes. Events. Let’s replace this case structure with an event structure. I’ll remove the One Button Dialog function, delete the case structure, and put down an event structure. Get rid of this timing. Now I need to tell the event structure what to look for. Right-click on the border and go to Edit Events Handled by This Case. Up comes my Edit Events Dialog Box. We’ll start here in the middle with Event Sources. These are all the places in LabVIEW that could generate an event. We see events coming from The Application, or LabVIEW, this VI, a couple other options, and what we’re finally interested in, controls on the front panel. So I’ll click on my OK Button and now in the Events window I see all the events that could be generated from this OK button. I see events that can occur from the keyboard, moving my mouse or clicking my mouse on the OK button, dragging over it, activating the shortcut menu, or a value change. Meaning that whenever the OK button changes value, an event is generated, that’s what I want. I now see the OK Button: Value Change as a configured event. I’ll click OK.
Now, when that Value Change event occurs, whatever is inside of this event structure, and particularly this specific case of this event structure, will execute. Naturally I’ll put this dialog function in here. Finally, I’ll put my latch action Boolean terminal inside of here as well. It’s not necessary to do this for all data types, but it is for latch action Booleans. See the text description of this video for the reason. Now let’s go to the front panel and run this VI again and note that the behavior is the same. Let’s click on highlight execution and watch this run…..this is pretty boring. But it’s a good boredom because we’re not constantly having LabVIEW poll our OK button. Instead, our code sleeps and waits for an event to occur, and immediately wakes up when it does. Let’s watch. That’s just better.
Now I’ve kept this extremely simple, and many of you familiar with event structures will note that I should also create a separate event for this Stop button. That’s true, so we’ll do that now. I’ll go to my event structure, right-click, Add Event Case, and this time, choose the Stop button and a Vaue Change. Ok, and now place the Stop button terminal inside the event case. And wire it out to my while loop conditional terminal. I’ll take it off highlight execution and run this again, and as before, flawless execution. And I’ll next show you how to program multiple events with this same event structure. I’ll be back next week to do that. Hopefully. I have a pretty busy weekend ahead…”

(end transcription)