The Queued Message Handler (QMH) template facilitates multiple sections of code running in parallel and sending data between them. Each section of code represents a task (e.g. acquiring data, logging data, user events) and is designed similarly to a state machine. Because of this design, you can divide each task into states.
The QMH template is a version of the Producer/Consumer design pattern, where the user interface (producer) produces messages and the tasks (consumers) consume them. However, in the QMH template, you also can produce messages from a consumer loop.
The QMH template is useful for applications where multiple tasks occur in parallel, often at different rates. For example, consider an application that continuously acquires, logs, and displays two signals: an RS-232 signal and an analog signal. These signals occur at different rates, so the application must have two loops that run in parallel. In addition, each loop is divided into the following states:
The application requires a responsive user interface; that is, users should be able to click buttons even while the application is executing another command. Therefore, the application requires a third parallel loop that continuously monitors the front panel for events, such as the following commands:
The QMH template provides a starting point for writing this kind of application.
This template repeatedly executes the following steps:
Notes:
Refer to the Continuous Measurement and Logging sample project for an example of adapting the QMH template to a measurement application.
LabVIEW includes a template project for this architecture which can be found by launching LabVIEW >> select Create Project >> Templates >> Queued Message Handler. This template includes one Event Handling Loop and one Message Handling Loop by default. You can update the template to fit your needs by following the tutorials in the table below.
The following table summarizes the design decisions you must make when modifying this template.
Design Decision | Example | Tutorial |
---|---|---|
You need to determine how many MHLs to add. Each MHL defines a task that executes in parallel with other tasks. | You have an application that acquires data and, in parallel, logs this data to disk. | Create a Message Handling Loop in LabVIEW Queued Message Handler Template |
For each MHL, you need to determine what message diagrams to add. | You want to separate the data acquisition task into three states: Initialize, Acquire Data, and Exit. Therefore, create these message diagrams in the MHL that acquires data. | Create a Message Diagram in LabVIEW Queued Message Handler Template |
You must determine what data the message diagrams of an MHL need. | Each message diagram of the data acquisition MHL needs access to a hardware reference. The Initialize message diagram needs to open this reference, the Acquire Data diagram uses this reference to acquire data, and the Exit message diagram closes the reference. | Defining Data that a Message Handling Loop Needs section in Create a Message Diagram in LabVIEW Queued Message Handler Template |
You need to determine when to execute each message diagram. A message diagram executes after its MHL receives the appropriate message. Therefore, you need to determine when to send each message to the MHL. You can send a message from a front panel control or from a message diagram. | You want to add a button that sends the Initialize message to the data acquisition MHL. | Send a Message to a Message Handling Loop in LabVIEW Queued Message Handler Template |
You need to determine if you want the Exit message to stop each MHL. The Dequeue Message VI uses this message because it is able to shut down an MHL. | You want each MHL to shut down when it receives the Stop message instead of the Exit message. | Send a Message that Stops the MHL section in Send a Message to a Message Handling Loop in LabVIEW Queued Message Handler Template |
When reading messages from the message queue, you need to determine if you want to ignore any specific errors. | When reading messages from the message queue, you want to ignore network timeout errors. | Handle Errors and Unrecognized Messages in LabVIEW Queued Message Handler Template |