Search code examples
event-handlinglabviewprogram-flow

LabVIEW: Correct way to control loop execution from an event structure


The program used has a a few control buttons (start, pause, stop, close) that are supposed to control the execution of a subVI test program. This is mainly a loop doing a certain task.

The question is: what is the proper approach to control the program flow (while loop) from the event structure? E.g. a start button press is supposed to start the flow - can I just wire this to the while loop? Wouldn't that mean polling the value? Or would it be passed only after the event is fired?

So far I have a loop for the events (multiple button value changes) and one other loop for the test program:

enter image description here

I assume something like the Queued Message Handler can be used for that? How can I achieve that the current loop run is stopped?


Solution

  • You need to use Event-driven producer-consumer pattern.

    Long story short: producer loop handles user events (like button press), and sends commands to consumer loop. Usually, communication between loops is done via queues. Queue sends cluster, which consists of state (which is either string or enum data type, LabVIEW queued state machine: enum or string?) and data (which is variant data type).

    Consumer loop enqueues messages from the queue, and based on state runs corresponding logic. Consumer loop has case structure, and selector is state value from the queue.

    There is a lot of different guides on the internet. Great overview is this presentation Introduction to LabVIEW Design Patterns, also you could check this one Event-driven producer-consumer state machine, or even video LabVIEW code: Event-driven producer-consumer state machine (walk-through), etc.