Advanced Event Handling with LabVIEW 7 Express

Publish Date: Sep 06, 2006 | 24 Ratings | 2.96 out of 5 |  PDF

Overview

It can be challenging to program an efficient UI that responds quickly and appropriately to user interactions. This task is even more difficult when you need the interface to behave differently under certain circumstances. The event-driven programming features introduced in LabVIEW 6.1 made great progress in providing LabVIEW developers tools to complete these tasks more easily and efficiently. LabVIEW 7 Express makes these tools more powerful and flexible, adding the ability to detect more user interactions, register for events programmatically, define and generate your own events, and receive notification of ActiveX events on ActiveX controls.

Table of Contents

  1. Event-Driven Programming in LabVIEW
  2. Addressing LabVIEW 6.1 Event-Structure Limitations with LabVIEW 7 Express
  3. Dynamic Events
  4. User-Defined Events

1. Event-Driven Programming in LabVIEW

Anyone can build a user interface quickly and easily using LabVIEW. With a few clicks of the mouse, you can have a button, table, and a graph on your front panel ready to display data and handle user interactions. It can be challenging, however, to program an efficient UI that responds quickly and appropriately to user interactions. This task is even more difficult when you need the interface to behave differently under certain circumstances. The event-driven programming features introduced in LabVIEW 6.1 made great progress in providing LabVIEW developers tools to complete these tasks more easily and efficiently. LabVIEW 7 Express makes these tools more powerful and flexible, adding the ability to detect more user interactions, register for events programmatically, define and generate your own events, and receive notification of ActiveX events on ActiveX controls.

Prior to the introduction of event-handling features in LabVIEW 6.1, if you wanted to program a VI to watch for and respond to interactions with its front panel, you would use a method called "polling." With polling, you keep track of the state of a control and periodically compare its current state to the saved state. In doing this, you can detect when a user has interacted with a control and changed its state. Unfortunately, this method suffers several limitations. First, storing the old state, getting the new state, and comparing the two requires work from the CPU proportional to the rate of polling. Second, this method is prone to error. Depending on the frequency of polling, the program might not detect user interactions that occur quickly. For example, if the user changes the values of a Boolean control from TRUE to FALSE to TRUE again between comparisons, a polling application will not detect a change in the value of the control. Last, this method limits you to a small number of detectable interactions -- primarily the change in the value of a control.

The event-driven programming features introduced in LabVIEW 6.1 addressed the shortcomings mentioned above. These features provided a reliable and powerful means of detecting user interaction with a VI's front panel. The event structure, which is the interface to these features, guarantees that you will not miss any user interactions, or “events." In addition, the event structure requires minimal use of the CPU to watch for events because it does not require a polling loop to detect changes in the state of the front panel. Instead, it sleeps, waiting for the system to notify it of actions you specify. Using the event structure, you can detect interactions that are not possible to detect with polling. These include the movement of the mouse and a user closing the front panel of a VI. While the Event structure made it easier to manage an application's user interface, if you are an advanced user, you most likely experienced limitations in the event-driven programming features as you tried to incorporate the features into more complex applications. One of these limitations was the inability to monitor events on multiple front panels. LabVIEW 6.1 restricted each VI to manage only its own user interface, making it impossible to create a common subVI to manage the front panels of several VIs. LabVIEW 6.1 also restricted you to built-in user interface events, so you could not generate your own events. New features introduced in LabVIEW 7 Express provide solutions for these limitations.

Back to Top

2. Addressing LabVIEW 6.1 Event-Structure Limitations with LabVIEW 7 Express


Dynamic event registration and user-defined events are the two primary event-related features added in LabVIEW 7 Express. These features target advanced users. With dynamic event registration, you can specify at run time which events you want to be notified of. Previously, you had to perform this task at edit time based on the events you configured your event structure to handle. Using dynamic events, your program can notify LabVIEW that it is or is no longer interested in a specific event on a specific object at any time during its run. It is now possible for your VI to execute with greater efficiency because LabVIEW will generate the specific events you want only when you want them, as opposed to the entire run of your VI – as was the case with 6.1. As a result, your VI executes event-handling code only when appropriate, rather than every time the user interacts with your panel.

Back to Top

3. Dynamic Events


Dynamic event Registration uses VI Server references to identify the objects for which it needs to generate events. You can pass these references to a subVI just as you would with property nodes. With this interface, you are no longer limited to performing all event handling tasks in your top-level VI. Any VI can monitor events on any object to which it has a reference -- regardless of whether it is located on the VI's front panel. You also can create a cleaner block diagram by moving your event-handling code to a separate VI. As a result, you benefit from user interface VIs that are more efficient and robust and allow for more flexible architectures and greater code reuse.

Back to Top

4. User-Defined Events


With user-defined events, you can create and generate your own events programmatically. Similar to queues and notifiers, you can use user-defined events to synchronize parts of your applications and transfer data based on the data type of the event you defined. Additionally, you can create your own events for actions unrelated to the user interface, such as a hardware trigger firing. Since user-defined events integrate into the event structure, you can have the same event-handling loop respond to these actions, as well as any user interaction with your front panel. This integration facilitates a common event-handling architecture in your application. You also can use user-defined events to simulate programmatic generation of user interface events by creating an event with the same data as the built-in event. For example, you can define an event called Programmatic Panel Resize , and configure your event structure to handle this event, as well as the built in Panel Resize event in order to share the event-handling code.. When you generate the user event, presumably when you programmatically change the bounds of the front panel, the event structure will respond to it just as it would respond to the interactive Panel Resize. Lastly, user-defined events allow for a cleaner overall architecture for more complex VIs. For example, you can easily implement a queued message-handler architecture using an event structure and user events. It is no longer necessary to have two separate loops - one watching for events and feeding messages to the queue and the second handling the queued messages.

Several other event-related enhancements are present in LabVIEW 7 Express. You now can programmatically generate value change events by updating a control's value using the new Value (Signaling) property. New events are available for you to detect when the user's mouse cursor enters or leaves a control's bounds, when the user resizes a VI's front panel, or when a user double clicks an item in a listbox. Multiple event structures now can register for the same filter event successfully. Previously, the first event structure succeeded while others failed silently, and LabVIEW did not notify them when the event occurred. Finally, you can register for native ActiveX events on ActiveX controls and respond to them with VIs that behave like C-style callback functions. LabVIEW automatically executes these Vis synchronously when an event occurs without any interaction with the Event structure. The callback interface is also available for regular event handling.

With LabVIEW 7 Express, event-driven programming with LabVIEW has matured to provide powerful and flexible tools for easily and efficiently designing your user interface. Although the major new event features target advanced users, all users will find more tools to help make their front panels more responsive and block diagrams simpler and more modular.

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit