LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Shutting down a queued event state machine with multiple parallel loops

Solved!
Go to solution

I am trying to find the best way to shutdown a program that consists of a queued event state machine architecture with multiple parallel loops. Can anyone recommend the best way to accomplish this in my attached VI? (From browsing the forum, this seems to be a common question but I haven't found a solution that works for me.)

I welcome any other feedback on my code as well, if anyone is willing to offer it.


 
My Program Requirements:


If the user presses the "Shutdown" button, the program should prompt the user with "Are you sure you want to stop the program?" and then either return to the Idle state or proceed to stop the program. Additionally if there is an error, the program should prompt the user with "Clear error and continue, or Stop program?" Then either return to the Idle State or proceed to stop the program.

Details of architecture:


The program consists of 3 parallel loops: (1) an Event Handling loop that enqueues various States to a State Queue, (2) a State Machine which enters the latest state that is dequeued from the State Queue, and (3) an Error/Shutdown handling loop which processes any errors in the Error queue.

During normal Shutdown, in the Event Handling loop the "Program.Shutdown" event case executes, and the States "Shutdown" and "Idle" are added to the State Queue. In the State Machine, the "Shutdown" state is invoked. A special error code "5000" is added to the Error Queue. In the Error/Shutdown handling loop, the "5000" error triggers a prompt that asks the user if they want to stop the program. If the user chooses not to stop, a notifier StopNotif is sent to the "Shutdown" state and "Program.Shutdown" event case with the notification "Go". If the user chooses to stop, the notifier sends the notification "Stop". The Event handling loop and State Machine are terminated if they receive the notification "Stop".

In case of error, the program behaves similarly: if the user chooses to clear the error and continue, the program returns to the "Idle" state.

HOWEVER - if the user chooses to stop the program, the program stalls. The notifier that is sent to stop the Event Handling Loop and State Machine cannot be read because the Program.Shutdown event case and the Shutdown state (which contain the "Wait for Notifier" function) are not active.

I have been able to activate the Shutdown state by enqueuing it in the Error/Shutdown handling loop. But I don't know how to activate the "Program.Shutdown" event programmatically, and thereby access the "Wait for Notifier" function inside it.

I tried to place the "Wait for Notifier" function outside the event structure, but then the Event Handling loop never completes. Placing timeouts on the "Wait for Notifier" and on the Event structure makes the program work, but I want to avoid using timeouts because I don't want to turn my event-driven program into a polling-based program. I'd also like to avoid using variables or property nodes to stop the loops, because that requires creating a control/indicator for something that the user doesnt need to interact with.

Thank you!

0 Kudos
Message 1 of 9
(4,277 Views)

1.  Don't release the notifier inside the event structure.  If that event case runs first, then the Release notifier is going to destroy all instances of it (because Force Destroy = True) perhaps before all the notifications have received it.

 

2.  Don't complicate things too much.  Just use the queues to send messages.  Don't put the shutdown in the error handling loop.  Have the event structure handle the question about whether to shutdown or not.  Then pass the message to the state machine loop.  Then have that pass the message on to the error handling loop.

Message 2 of 9
(4,271 Views)

Don't put the shutdown in the error handling loop.  Have the event structure handle the question about whether to shutdown or not.  Then pass the message to the state machine loop.  Then have that pass the message on to the error handling loop.

 

That...makes a lot of sense. I'll try that.

 

I would still like the user to be prompted with "Continue or Shutdown Program?" after an error occurs. Can I get the event structure to respond when the user interacts with a dialog box in the error-handling loop? Dialog boxes aren't listed in the Event Sources column of the event structure's Edit box.

0 Kudos
Message 3 of 9
(4,267 Views)
Solution
Accepted by topic author MaryamAli

First of all, close out the notifier outside of the loops with your queues.  Secondly, you need to use a User Event to send the message to the event structure loop in order for it to stop in the case of stopping on an error.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 9
(4,265 Views)

Thanks for telling me about User Events. That's exactly what I needed.

0 Kudos
Message 5 of 9
(4,203 Views)

Then you should mark crossrulz message as the solution to your question rather than your own thank you message.

 

First go to the options menu to the upper right of your post and select "unmark as solution".

Message 6 of 9
(4,197 Views)

Is it possible to mark two different posts as solutions? You and crossrulz both contributed to my answer.

0 Kudos
Message 7 of 9
(4,177 Views)

No, you can only mark one as a solution.  So mark whichever one you feel most helped you.

 

I figured it would be crossrulz's post since you were thanking him about using user event's.

0 Kudos
Message 8 of 9
(4,171 Views)

Thanks for clarifying that, Ravens Fan.

 

I marked crossrulz as the solution for pointing out User Events to me.

 

I also adopted Ravens Fan's suggestion to keep the Shutdown procedure out of the Error Handling Loop. This has simplified the architecture by eliminating the need for Notifiers. Instead, I have used booleans in case structures to stop the Event Loop and State Machine, and Release Queue to stop the Error Handling Loop.

 

For reference, I'm attaching my corrected code.

 

Thank you to everyone who helped!

Message 9 of 9
(4,167 Views)