Insights · Test & measurement

State machine vs QMH:
how to structure LabVIEW code

What NI's Simple State Machine and Queued Message Handler templates actually do, where they differ on a test system, the pitfalls to check in inherited code, and when to move to DQMH or the Actor Framework.

Published 5 Dec 2026 · Live Experts

The short answer: a state machine runs one task, one step at a time, in a single loop. A queued message handler (QMH) splits the application into loops that run in parallel and pass messages through queues, so the user interface can keep responding while other work goes on. NI itself calls the QMH "a more sophisticated version of the State Machine" that adds flexibility and also complexity (NI: state machines). If your program does one thing at a time and the operator only acts between steps, a state machine is enough. If it has to acquire, log and respond to the operator at the same time, start with a QMH or something built on it.

What a state machine is in LabVIEW

NI describes a state machine as the block diagram version of a state diagram or flow chart, where each state has the logic to decide which state runs next (NI: design considerations in LabVIEW). NI's Simple State Machine template builds it from a few parts (NI: state machines):

  • A While Loop that keeps running states until the application stops.
  • A Case structure with one case per state.
  • A shift register that carries the next state into the following iteration.
  • A typedef enum (State.ctl) listing the valid states, and a data cluster (Data.ctl) for values the states share.
  • A Wait for Event state with an Event structure that handles front panel input.

NI lists the jobs it suits, such as a tabbed dialog, logging a single measurement, or process testing (NI: state machines). Its limits are stated just as plainly. Only one state runs at a time, and because there is a single loop, every task runs at the same rate. User input is only recognised in the Wait for Event state, so the state machine must be in that state for the operator's click to be accepted. So if a Measure state takes thirty seconds, for example, a click on Stop isn't handled until it finishes.

In its favour, the states are a typedef, so every copy updates when you add one, and only the Stop state ends the application, so shutdown code runs to completion (NI: state machines).

What the queued message handler is

The QMH is a LabVIEW project template. NI describes it as a version of producer/consumer (NI: using a queued message handler), the pattern that decouples code producing and consuming data at different rates through a first-in, first-out queue (NI: producer/consumer architecture). In the QMH template:

  • An Event Handling Loop (EHL), the producer, responds to front panel events and puts messages on a queue.
  • A Message Handling Loop (MHL), the consumer, reads the oldest message on each iteration and runs the matching case, called a message diagram.
  • Each message queue belongs to one MHL, and an application can have several MHLs, for example one for acquisition and one for logging.
  • A message diagram can enqueue further messages, so a consumer loop can produce messages too.

NI says each task in a QMH is designed much like a state machine, with each message diagram matching a state the task can be in (NI: using a queued message handler). The difference is that queued messages, not the previous state, decide what runs next. NI recommends it where several tasks run in parallel, often at different rates, such as acquiring, logging and displaying two signals while the user interface stays responsive.

One detail matters in practice: in NI's template, a message is a string that has to match the name of a case in the MHL (NI: using a queued message handler), not a value picked from an enum.

Side by side

Responding to the operator while working

In the Simple State Machine, input is only read in Wait for Event (NI: state machines). In the QMH, the Event Handling Loop keeps accepting user events and queuing messages while a message handling loop is busy (NI: using a queued message handler). The message itself still waits its turn: each MHL reads the oldest message on each iteration, so a long message diagram delays what is queued behind it.

Parallel tasks at different rates

A single-loop state machine runs everything at one rate (NI: state machines). In a QMH you add an MHL per task, each with its own queue (NI: using a queued message handler), which is why NI points to the QMH or the Actor Framework for multi-rate work.

Adding features later

In a state machine you add a state to the typedef enum (every copy updates) and decide which state comes next from it (NI: state machines). In a QMH you add a message diagram, or a new loop with its own queue; NI links a tutorial for each step (NI: using a queued message handler).

Errors and unexpected input

NI's state machine guide asks you to decide up front which errors could occur and how the application should respond (NI: state machines). The QMH needs one more safeguard. Because messages are strings, not enum values, NI advises a Default message diagram in every MHL Case structure to catch messages with no matching case, and shows how to ignore expected errors such as network timeouts (NI: handle errors and unrecognized messages).

Learning curve

The state machine needs only a loop, a case structure, a shift register and an enum. NI's QMH tutorials call it an advanced architecture (NI: handle errors and unrecognized messages).

Pitfalls worth checking in code you inherit

If you have taken over a program built on either pattern, check the points NI's own material flags, including its queued state machine example for RIO hardware:

  • Mistyped messages. A misspelled string message matches no case. NI's queued state machine example (from its RIO developer guide) uses the Default case to trap it and raise an error (NI: queued state machine).
  • Blocking code inside the loop. In a queued state machine, code that blocks data flow stops the loop from responding to new messages, so it looks locked up (NI: queued state machine).
  • States that repeat. A task that has to keep running across iterations, such as continuous acquisition, has to enqueue its own state again, with a Wait to set the loop timing (NI: queued state machine).
  • Everything on one diagram. NI warns this makes it impossible to develop, test and debug features in isolation (NI: design considerations in LabVIEW).

When to go beyond the QMH

DQMH

DQMH is a free framework, source included (DQMH), that extends the queued message handler pattern with event-based communication and tools (DQMH: the framework). It is maintained by the DQMH Consortium and installed through VI Package Manager, for LabVIEW 2016 or later (DQMH: download, NI Tools Network: DQMH). Per its documentation and listings, it adds:

  • Modules. Each module does its own work and exposes an API; other code asks it to act through a request and listens for broadcasts when something changes (DQMH: the framework).
  • Singleton and cloneable modules. A singleton for a single responsibility such as a logging service; cloneable modules for several instances of one design, such as independent instruments or acquisition channels (DQMH: the framework).
  • Scripting tools to add, rename, remove and validate modules and events while keeping the framework's conventions (DQMH documentation).
  • A generated API Tester to exercise a module before connecting it to the rest of the system, and to watch its events and messages (DQMH: the framework, VIPM: DQMH package).
  • Documented support for unit testing and TestStand integration (DQMH documentation, NI Tools Network: DQMH).

Actor Framework

The Actor Framework ships with LabVIEW, with a template and a sample project in the Create Project dialog. You don't build it by dropping subVIs; you create child classes of Actor.lvclass and override its methods (NI: Actor Framework). NI describes it as an object-oriented implementation of the QMH, where child actors inherit the behaviour of parent actors, and says success with it depends on a strong foundation in object-oriented programming (NI: design considerations in LabVIEW).

For a buyer, the practical difference is the team: DQMH builds on QMH ideas and adds tooling, while the Actor Framework asks for a strong grounding in object-oriented LabVIEW, which NI says success depends on.

How to decide

  1. Must it do two things at once, such as acquire while logging, or respond to Stop mid-measurement? If not, a state machine is enough.
  2. Do tasks run at different rates? If yes, each needs its own loop, which points to a QMH or beyond.
  3. Will new instruments or stations be added later? Plan for modules from the start, with DQMH or the Actor Framework.
  4. Who maintains it after delivery? Pick what your own engineers can read and extend.

What we use

We match the architecture to the job. Our LabVIEW example is a 2026 data acquisition platform for a US engineering client, which runs on the Actor Framework because new instruments were expected; it was delivered in six reviewed milestones and led to a follow-on build. More on our work is on LabVIEW development and data acquisition and test systems. If you are budgeting, see how much LabVIEW development costs, and for logging choices, TDMS vs CSV.

Questions engineers ask

What is a queued message handler in LabVIEW?

A LabVIEW project template built on producer/consumer: an Event Handling Loop turns user events into queued messages, and one or more Message Handling Loops read them and run the matching code (NI: using a queued message handler).

Is the QMH a state machine?

Not in the template sense, but close. NI says each QMH task is designed similarly to a state machine, with each message diagram matching a state (NI: using a queued message handler), and calls the QMH a more sophisticated version of the state machine (NI: state machines).

Actor Framework vs DQMH: which should I use?

Both split an application into parallel parts that talk by messages. The Actor Framework ships with LabVIEW and is object-oriented, so it needs OOP skills (NI: design considerations in LabVIEW, NI: Actor Framework). DQMH is a free add-on installed through VI Package Manager that extends the QMH with event-based modules, scripting tools and an API Tester (DQMH: the framework, DQMH: download). Choose the one your maintainers know or can learn.

Get a fixed price for your project

Describe what you want built and within two working days you get a free written plan with milestones, risks and a fixed price. Clients rate our work 4.9 out of 5 across 354 verified Freelancer reviews.

Tell us what you're building.

Send a short brief. We reply with questions or a fixed-price proposal. If AI isn't the right answer, we'll say so.