Showing results for 
Search instead for 
Did you mean: 

queued state machine

Go to solution

I have a queued state machine.

In step1 and step 2 a sub Vi, which takes 1000 ms, decides which step to goto next.

That’s okay.

My problem arises, when I try to invoke the process by the stop button.

Because the program is inside subvi 1 or subvi 2, and have not yet reached the enqueuer element the queue suddenly contains 2. elements and the process goes from step1 (or step2) - end - step 1 – end ….

Which way would be the best way to fix the problem?snip.png

0 Kudos
Message 1 of 19

another snipsnip2.png

0 Kudos
Message 2 of 19

Without getting into the subVIs, the first change you'll need to make is moving the While Loop conditional boolean values to be set to something other than False. Best approach is to move this value inside the Event handler in the top loop and inside the Case Structure in the bottom loop. 


As it stands now neither loop will ever exit.

0 Kudos
Message 3 of 19

Only 5 edits are needed:


  1. Enqueue "End" at Opposite End rather than the back of the queue
  2. Move boolean constant inside "Stop" VC Event and make True to stop the event loop
  3. Kill "Junque Code" in consumer End Case
  4. Move Boolean constant inside "End" case and make True to stop the HM loop.
  5. Make Boolean tunnel Use Default if unwired to let MH Loop continue til End is processed

Since you named the queue you may also wish to "Force Destroy" the queue when you release it in case someone else opened a reference to "state" queue so that code errors out appropriatelty.  (or don't name a queue that is only used locally)


Also, you will want to type def that state enum to make your life a lot easier in the future

"Should be" isn't "Is" -Jay
Message 4 of 19

It seems like you are just experimenting around right now, but when you do make a real application, make sure to look at the Queued Message Handler template that ships with LabVIEW. You will find some features that you may want to incorporate into your project:

  • Error handling
  • User event to stop event handler
  • Initialization Case
0 Kudos
Message 5 of 19

I know. This was just a very quick example to visualize my problem

 I try to look at the template

0 Kudos
Message 6 of 19


I didn't find anything the solve my poblem with mixing of to states in the queued state mashine

0 Kudos
Message 7 of 19
Accepted by topic author Michael.Koppelgaard

What you have is a Queued Message Handler (QMH).  It should only receive commands from the outside world.  It should not enqueue onto itself due to these race conditions that you are running into.  If you want an actual Queued State Machine (QSM), then you need a queue for maintaining your states and another queue for receiving messages.  Part of your sequence of states should be a state for checking for messages.  Alternatively, you could use the timeout capability of your dequeue.  You just store in a shift register which state you go into if no message was received.


And the loop that is reading from the queue should be the one to close it.  As it stands, you will almost guaranteed to get an error code1 after the producer loop stops and the consumer tries to read the stop command since the producer destroyed the queue.  And with that error, you will get the default value for element, which will be whatever is in spot 0 (I am guessing that is Step 1).

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 8 of 19

Thank you guys. I can see that I have a lot to learn.

I will indeed try to do so. I would like to return tomorrow with a corrected example – then you are free to make fun of me if it’s totally wrong J

0 Kudos
Message 9 of 19

While I may use humor to point out flawed code, I try to never make fun of a poster wishing to learn.  (Besides, you should see some code I wrote years ago)

"Should be" isn't "Is" -Jay
0 Kudos
Message 10 of 19