You are on page 1of 16

Inside LabVIEW: Event-driven Programming

National Instruments Confidential

Overview
What is event-driven programming How to use events in LabVIEW Caveats and common mistakes to avoid

National Instruments Confidential

What Is Event-driven Programming?


Order of execution is influenced by events on the user interface Code executes in response to change in state of system No need to poll for change in state Event loops versus call-backs

National Instruments Confidential

Introducing... the Event Structure


What is it?
New structure node with multiple sub-diagrams, like a Case Structure

How does it work?


Goes to sleep until an event occurs on the panel Automatically executes the right event case (sub-diagram) for the event Structure completes when it finishes handling exactly one event; does not implicitly loop

National Instruments Confidential

Event Structure Basic Usage


Normally used in a While Loop Sleeps and handles exactly one event per iteration Event Data available in terminals on structures left border Can right-click on event data and show only the terminals you need

National Instruments Confidential

Event Structure Configuration


Event Structure is configured via dialog by rightclicking its border and using Edit Events LabVIEW 6.1 events are divided into three classes:
VI: Mouse Down/Move/Up, Key Down/Repeat/Up, Menu Selection (App/User), Panel Closing Control: Mouse Down/Up, Key Down/Repeat/Up, Value Changed Application: Application Exiting

National Instruments Confidential

Notify vs. Filter Events


Events are divided into two categories: Filter and Notify Notify Events are after-the-fact notification that something happened Filter Events allow you to change the events data as it happens, or discard the event entirely
Key events, Mouse events, Menu events, and the front panel window closing event are all filter events

Filter Event Examples:


Make string controls that map upper- to lower-case Bring up a confirmation dialog when the user tries to close the panel

National Instruments Confidential

Parts of an Event Structure


Event Selector Label identifies currently shown event case Timeout - value in milliseconds to wait for events; default value is 1 (indefinite) Event Data Node - data unique to each event case Event Filter Node - allows you to change or discard event data for a Filter event before the event is completely processed Defaulted Tunnels - new kind of tunnel which allows unwired cases to provide default values

National Instruments Confidential

Simple Event Demos


Event tracking of front panel controls
Event Tracking.vi (LabVIEW 6.1 shipped example)

Confirmation dialog on closing of VI's front panel


Event Conditional Stop.vi (LabVIEW 6.1 shipped example)

National Instruments Confidential

Event Registration and Panel Locking


Events are registered when a VI with an event structure on its diagram becomes reserved When an registered event occurs, it is queued until the event structure(s) configured for it executes It is not possible to miss events or process them out of order By default, front panel is locked until the event is handled
You can turn off locking, but only for Notify Events

Events are unregistered when the VI becomes idle

National Instruments Confidential

Limitations of Events in LV 6.1


Static registration only; cant register on the fly Events are generated only by direct user interaction
No events on device I/O, etc.

Not possible to programmatically fake events in 6.1 (without cheating)

National Instruments Confidential

Caveats and Common Mistakes


Value Changed events and Latched Booleans
Only reading the front panel terminal will reset the Boolean

Stop Booleans and the event-handling loop


Dont mix event-handling and polling in the same loop

Programmatic changes DO NOT generate events Static events generated for entire duration of VI
Make sure Event Structures are always in loops to handle events

An event-handling loop is not a state machine

National Instruments Confidential

Events and UI Design Patterns


Sophisticated UIs which use a state machine or queued message handler design can be easily retrofitted to use events Simply replace the idle case in your state machine which does polling with an event structure configured to detect the same UI changes

National Instruments Confidential

Who Can Best Take Advantage?


Events may benefit any application that:
polls for value changes on multiple controls hides the window close box uses the menu primitives uses keyboard.llb needs to clean up or request confirmation when LabVIEW shuts down

Events work with Remote Panels, with either a full LabVIEW client or the web browser plug-in

National Instruments Confidential

Resources
Shipping Examples (examples/general/uievents.llb) User manual and on-line reference
LabVIEW Technical Resource article (http://ltrpub.com) A Powerful New Tool for UI Programming (Vol. 9, Issue 4) Articles on NI Dev-Zone (http://zone.ni.com - search for events and event-driven) See http://www.ni.com/info and enter code niweek02 for online access to these slides and further information

National Instruments Confidential

Questions/Comments?

National Instruments Confidential

You might also like