LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to control separate while loops with one stop button

Hi, 

I'm trying to control separate while loops with a single button, and I'm not sure how to achieve this. 

 

Here's what I've got going: 

- I have one event structure.

- When "A" happens, the event structure triggers a while loop "While A."

- When "B" happens, the event structure triggers a while loop "While B." 

 

So these loops aren't ever triggered in parallel, but I would like to still be able to stop them with the same control. How should I go about doing that? 

 

Here's some basic code illustrating what I'm trying to get working: 

rmigliori_0-1602789553095.png

 

rmigliori_1-1602789566069.png

 

 

Thanks, happy to answer any questions. 

 

0 Kudos
Message 1 of 9
(1,537 Views)

Interactive while loops typically belong outside the event structure, not inside it.  Can you tell us what you want to do, not how you want to do it?

 

(If you have seperate loops with an event structure inside each, both event structures can listen to the same stop button to stop its loop.)

0 Kudos
Message 2 of 9
(1,523 Views)

Without knowing more about your code it is hard to give you an answer other than that you should not have a while loop inside of your event structure. You should consider a different architecture for your program. There are a number of architectures which may be suitable, but it's hard to tell which is the most appropriate for your application without further information.

0 Kudos
Message 3 of 9
(1,461 Views)

I changed things around to look like this, taking the while loops outside the event structures.

 

rmigliori_0-1603316064091.png

 

This almost works as I want it to, except of course the stop button won't stop either loop while they are running, which is kind of the main issue.

 

I don't really understand why it's so difficult to get one control to stop separate loops. Frustrating.

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

Your new version with two event structures is actually worse...

 

If you were to go back to the original diagram you posted, the "easy" solution would be to to right-click the terminal for the control you want to use to stop the loop, choose "Create local variable", and stick the local variable it makes into the other while loop, then right-click it to select "Change to read", and connect it to the Stop terminal.

 

I say that this is the "easy" method because the previous two posters are correct that this is generally not good practice.  An event structure should usually execute near-instantly, so it's available to happen many events happening in a short time if that's what the program needs.  Read up on "Producer-consumer" architecture in LabVIEW here, and consider using something like that instead to make a better program for the future.  If you go to LabVIEW's help menu and choose "File --> New..." and then from that menu look under "VI" for the "From template" section, then "Framework" and finally "Design Patterns", under that there will be a choice for "Producer-consumer design pattern (events)" that you can select and it will generate a "starter" VI for you to change that has some simple code you can experiment with and modify to run your loops in the "consumer" portion of the VI.

0 Kudos
Message 5 of 9
(1,412 Views)

@rmigliori wrote:

I changed things around to look like this, taking the while loops outside the event structures.

 

rmigliori_0-1603316064091.png

 

This almost works as I want it to, except of course the stop button won't stop either loop while they are running, which is kind of the main issue.

 

I don't really understand why it's so difficult to get one control to stop separate loops. Frustrating.


This doesn't work at all. I do recommend that you do some of the training exercises. The recommendation that Kyle97330 gave you is a good architecture for more advanced projects, but at least for your simple program there is no need for two loops. I've created a little snippet below to show you how it could be done. I've also attached it saved to LabVIEW 2017.

 

Two Events One Looop.png

0 Kudos
Message 6 of 9
(1,399 Views)

@rmigliori wrote:

 

This almost works as I want it to, except of course the stop button won't stop either loop while they are running, which is kind of the main issue.

I don't really understand why it's so difficult to get one control to stop separate loops. Frustrating.


They key concept is dataflow and it is very easy to understand once you do some of the tutorials. Your code shows that you don't understand it at all yet.

Why don't you have a toplevel loop? When your program is run all three buttons are read once (and never again!) and both event structures are armed. Once one of the event buttons changed value (either false-true or true-false) a true is put on the output wire allowing the dependent case structure to start executing (Since the value can never be false, the case structure could be omitted and the wire just connected to the while loop to create a data dependency). Same for the other branch. Since the stop button has only been read once, whatever its value was at the start of the program is seem by the while loops.

 

Dataflow is the most powerful aspect of LabVIEW, so you simply need to embrace it instead of fight it.

 

Look at Johntrich's example above.  It probably needs a bit more code to work the way you want. Note that the cases don't contain inner loops. You also need a way to turn off each case once the condition is reached. See how far you get.

 

Next time you have a problem, please attach your actual VI. Images are typically insufficient to full y describe your code (we cannot tell what the mechanical action of your Booleans is, we cannot tell what's in the other case, etc.)

0 Kudos
Message 7 of 9
(1,373 Views)

See if this can give you some ideas (probably needs a bit more code to be useful).

:

altenbach_0-1603392228999.png

 

0 Kudos
Message 8 of 9
(1,369 Views)

You will probably need to use notifiers or queues, I prefer using queues in order to achieve that, and also adding one loop for event handler; it is the simpliest version of the queued message handler arquitecture, where you have one loop to acquire user interactions and two loops or more for each task; I'll add a simple example:

Tasks exampleTasks example

0 Kudos
Message 9 of 9
(1,362 Views)