Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 36 ยท Dataflow and Structures

Event Structure and Timing

Respond to user actions with the LabVIEW Event structure, avoid polling, use value change and timeout events, and time loops with wait and elapsed-time VIs.

A user interface needs to react when someone clicks, types, or closes a window. The simplest way is to poll: keep checking every control in a loop. LabVIEW offers a better tool, the Event structure, which sleeps until something happens. This lesson covers that structure and the timing functions that go with it. The product itself cannot run here, so the stand-ins are JavaScript models, not LabVIEW.

Polling versus events

A polling loop reads a Boolean control 20 times a second and acts if it is true. It works, but it wastes processor time, can miss a click that happens between two reads, and is awkward when many controls exist.

An Event structure is different:

  • It waits with almost no CPU use.
  • It runs exactly one event case per event, then exits so the loop can go around again.
  • Events are queued, so a quick click is not lost.

Place an Event structure inside a While Loop. Each iteration, it sleeps until an event fires or a timeout passes, runs the matching case, and returns to the loop.

Building one

  1. Right-click the border of a While Loop area and drop Programming > Structures > Event Structure.
  2. Right-click the border and choose Add Event Case.
  3. In the dialog, pick the Event Source (a control, the panel, a menu, or the application) and the Event (for example Value Change).
  4. Place the code for that event in its case.

Common events:

Source Event Meaning
A control Value Change The user changed the value (not code)
A control Mouse Down / Key Down Raw input events
Panel Panel Close? The user tries to close the window
Timeout Timeout Nothing happened for the given ms
Application Application Instance Close? The program is closing

Value Change fires only when the user changes the control, not when your own code writes to it with a property node or local variable. This prevents loops where your code and your event keep triggering each other. A programmatic change can generate a Value Change event if you use the Value (Signaling) property.

Event data nodes

Every case has an Event Data Node on the left. Resize it to show fields such as NewVal, OldVal, Source, Time. Use NewVal to avoid reading the control with another wire, which could return a newer value from a second click.

Timeout

The timeout terminal at the top left sets how long the structure waits in milliseconds. -1 means wait forever. A value such as 100 adds a Timeout case that runs every 100 ms when nothing else happens, handy for updating a clock or reading a sensor without a separate loop.

Locking

By default the front panel is locked while an event case runs: the controls cannot change until it finishes, so values stay consistent. Long work inside a case freezes the UI. Keep event cases short, and hand slow work to another loop. This is the producer/consumer idea you will see in later chapters.

Event loop

Sleep Wait for an event
Event User clicks
Case runs Short code
Loop Back to sleep

Keep cases short.

1 / 2

Rules to remember

  • One Event structure per loop. Two in the same loop can deadlock, because both wait for a lock.
  • A control tied to an event case should not be the only way to stop, or the loop can hang if the UI is dead. A Timeout and Panel Close? case give you safe exit paths.
  • A Stop button's Value Change case writes True to the loop's stop condition.
  • Do not place a Wait in the loop; the structure already waits.

Timing functions

LabVIEW offers several time tools in Programming > Timing:

  • Wait (ms) and Wait Until Next ms Multiple (see while loops and for loops).
  • Tick Count (ms) returns a millisecond counter that wraps around after about 49.7 days (a U32). Subtract two values to measure an interval; unsigned arithmetic handles the wrap.
  • Get Date/Time In Seconds returns a timestamp.
  • Elapsed Time express VI measures time since a start and signals when a target time passes.
  • Time Delay and Time Delay (Express) are simpler waits.
  • Timed Loop (a structure) offers precise timing and scheduling, mostly used with real-time targets.

Measuring a code section

To time something: read Tick Count before and after (wire the "before" count to the node's error or data input to enforce order), then subtract. Use a flat sequence or error wire so the second reading happens after the code.

Stand-in 1: an event queue

The stand-in below models how an Event structure queues events and handles one per iteration. The "loop" keeps asking for the next event; if none arrive it times out. This is a model, not LabVIEW's implementation.

JS Stand-in: events queued, one case per iteration

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in 2: measuring an interval with a wrapping tick counter

This shows why subtracting unsigned tick counts survives the 32-bit wrap. JavaScript's >>> 0 forces an unsigned 32-bit result, imitating a U32 subtraction.

JS Stand-in: U32 tick count wrap-around

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of an Event structure over polling?

    Events use almost no CPU while waiting and are queued.
  2. 2. What does a timeout value of -1 mean?

    -1 means no timeout, so the structure waits until an event arrives.
  3. 3. Does Value Change fire when your own code writes to the control?

    Plain writes do not generate the event; the Value (Signaling) property does.
  4. 4. Why keep event cases short?

    A long case freezes the UI until it finishes.

Summary

  • An Event structure inside a While Loop waits for user events without polling.
  • Events are queued; each iteration runs one case. Add Timeout and Panel Close? cases for safety.
  • Value Change is for user changes; use event data (NewVal) instead of re-reading controls.
  • Use one Event structure per loop and keep cases short.
  • Tick Count (U32 ms) and Elapsed Time measure intervals; unsigned subtraction handles the wrap.

Next: Numeric and Boolean Data