12-04-2012 11:56 PM
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
12-05-2012 12:00 AM - edited 12-05-2012 12:01 AM
Look at a notifier.
Or look at a functional global variable (aka Action Engine).
12-05-2012 12:48 AM
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).
12-05-2012 01:11 AM
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.
12-05-2012 08:11 AM
It looks like you already have all the pieces, you just need to modify them slightly. I would do the following:
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.
12-07-2012 02:12 PM
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