Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I properly terminate a program that consists of several while loops?

I have a program that uses a while loop, and then when I press a boolean, it jumps out of that while loop and exectues a new one (so I have two while loops in series).  When it reaches the end of that while loop, the program terminates.  But is there a way to terminate the entire program safely, regardless of whether I'm in the first while loop or second while loop?  If I press the abort button or insert the STOP.vi it causes the program to crash because I use a USB camera in my program.

 

I tried drawing a bigger while loop around the whole structure.  I figured if I did that and created a boolean to stop the big while loop, the entire program would safely terminate.  But no luck.  It seems that if the program is inside one of my embedded while loops, pressing the stop button on the bigger while loop has no effect, which seems odd to me.  Any advice?  

 

Jessica

0 Kudos
Message 1 of 8
(4,488 Views)

There are many ways to solve this problem.

 

One way is to use an event structure in the first loop.  It can have a timeout case with zero msec and a stop button changed case.  In the timeout case, output false.  In the stop button case, output the current state of the button.  In your second loop, just read the stop button.

 

Another method is to create two stop buttons, one for each loop.  If you get fancy, you show the control for the active loop and hide the control for the inactive loop.  To the user, it looks like there is only one stop button.

 

Another method is making the boolean switching instead of latching.  You can read it in the second loop using a local variable this way.  You do need to pop up the button when you are done by setting it false, since it won't do it by itself.

 

Bruce

Bruce Ammons
Ammons Engineering
0 Kudos
Message 2 of 8
(4,485 Views)
Something else to point out is the While Loops are effectively Do-While loops, meaning they'll always execute at least once. Just keep that in mind if it affects your application.
Jeff | LabVIEW Software Engineer
0 Kudos
Message 3 of 8
(4,460 Views)

The forum folks have been hammering me over this one for a while  now.  I have finally (not suggesting you do this, just that this is what they have convinced me to do...) is to handle all the gui interface in a loop that then sends a state name to run my other functions.  Sounds like you have two loops going on, so add another loop to be the producer of state calls and the other two loops be the consumer of the state calls.  If you hit the stop button, create two stop queue items, one each of the state loops, and put the stop on the top of the queue.  Each loop will then switch to the case "stop" and that should kill its operation...Let me see if I can whip up an example with just the stops.  Back in a minute.

 

Hummer1

 

0 Kudos
Message 4 of 8
(4,451 Views)

This is a do nothing example showing how you might pass a stop command along in queues to stop a multiloop state machine...Turn on the lightbulb and put the foreground and background side by side then run it and be amazed. 

 

Hope you find this useful.

 

Hummer1

0 Kudos
Message 5 of 8
(4,449 Views)

Thanks Hummer, but any chance you could save the VI in a previous version?  I only have LV 8.5 so I can't open any LV 9 files.

 

Jessica

0 Kudos
Message 6 of 8
(4,446 Views)

Can't get to 8.5, but here is a picture.

 

Hummer1

0 Kudos
Message 7 of 8
(4,442 Views)

Sameidea, but if you do this with Notifiers then you only have to hve one notifier for all loops. Each read can get the notifier status and doesn't remove it so another loop can't get the value. I have this running across apps with 6 or 7 loops and works fine. Just remember to only clear the notifier when all loop s have finished.

 

Mike

0 Kudos
Message 8 of 8
(4,424 Views)