LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Graceful, asychronous multi-loop stop?

How do I get a program structured like this to gracefully stop at the push of a global STOP button?

 

1. Set up serial port

 

2a. Producer loop polls the serial port

2b. Event structure handles UI events

2c. Consumer loop parses serial message and queues up data

2d. Consumer loop assembles data, processing, and presentation

 

3. Send a final message over serial, save collected data to file, tear down serial port, collect errors, etc.

 

I would like the stop button to act asynchronously, so that all the consumer loops abort as soon as possible and we move to state 3 where files and ports get closed gracefully.

 

Much appreciated!

 

M

0 Kudos
Message 1 of 6
(3,055 Views)

Look at a notifier.

 

Or look at a functional global variable (aka Action Engine).

0 Kudos
Message 2 of 6
(3,052 Views)

To stop the two consumer loops, inside the event case for the Stop button I would simply destroy the queues.  When the consumer next tries to dequeue, it will return immediately with a specific error (I don't remember the error code off-hand) which you can then use as the stop trigger.  To stop the producer loop that's polling the serial port, I'm with RavensFan - use a notifier, and set the timeout to 0 so it doesn't affect your existing timing.  To make sure all loops have exited before reaching the 3rd state, run a wire out of each loop (even if it carries useless data) that feeds into the state 3.  One way to do this is with merge errors - but make sure you clear errors (such as the one that the dequeue will generate) so that they don't prevent state 3 from executing.  Another is to wrap all the code for state 3 in a case structure or single frame of a sequence structure, and connect wires from the loops to the edge of the structure (they don't need to connect to anything inside the sequence, they just enforce execution order).

0 Kudos
Message 3 of 6
(3,040 Views)

I have used variables to stop multiple loops.

In my program I have to run everything independent so I have A manin while loop and placed several while loops inside it for separate process and interrelated processes.

Now use a local variable to all loop stops or a global variable to stop subvis.

Hope this helps.

Labview user
0 Kudos
Message 4 of 6
(3,028 Views)

It looks like you already have all the pieces, you just need to modify them slightly.  I would do the following:

 

  1. On STOP, the Event Structure loop sends a message to the serial port to stop immediately and to send an END message to the parsing loop.  The Event Structure loop should exit - stopping it.
  2. When the serial port loop gets the STOP message (probably will require some programming - a queue with a zero timeout works well here, or you can use the timeout as your poll interval), it sends its message down the serial port and sends an END message to the message parsing loop.  It then exits.
  3. When the message parsing loop gets the END message, it sends an END message to the processing/presentation loop.  This is after the current data, so nothing should be lost.  The message parsing loop then exits and cleans up the queue to the serial port loop.
  4. When the processsing/presentation loop gets an END message, it flushes all data to disk, cleans up all references, and exits.

This process gives you an orderly shutdown with no lost data.  If you want an ABORT, it is easy to add by sending an END command from the Event Structure loop to all the other loops when it occurs.  Further enhancements include adding a watchdog loop of some sort to make sure nothing hangs in the shutdown process.

 

Good luck.  Let us know if you need more info.

 

0 Kudos
Message 5 of 6
(3,010 Views)

Thank you to everyone that replied! This is a really helpful post already because of the availability of options for solving this problem.

 

For my purposes, I chose to destroy queues on the STOP button event. One caveat with this option is that you actually have to try and do something with the queue before it generates an error, so my first operation within a consumer loop is sitting outside of the Error/No Error structure. I will try and implement some of the more elegant suggestions if time permits.

 

If I really step back from this problem, I should probably do away with the STOP button all together and trigger the stop when the user selects File > Exit.

 

Anyway, many thanks!

 

M

0 Kudos
Message 6 of 6
(2,969 Views)