LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Running a vi with a while loop

Hello All,

I have a main program with a while loop and a few event cases. On one of the event cases I'm running a vi with a while loop. When I run the vi with the while loop I have no control over the main program until the vi finishes its application.
I want to be able to control the main program while the vi is running and I also want to be able to abort the vi while it is still running.
Can I run the vi in parallel with the main program and that way I will have control over the main program while the vi is excuting? If that is possible, how do I stop the vi?

I'm using Labview 8.

Thank you,
Shafran
Message 1 of 9
(3,592 Views)
What you are seeing is by design. The event case cannot complete until the while loop completes. You have 2 immediate options:
  • Have this internal while loop as a separate, independent while loop that runs in parallel to your current while loop. This separate while loop would need to run all the time, so you would need a case structure to control when the VI runs. This basically gets you closer to a producer-consumer architecture.
  • Put the while loop code into a wrapper VI and launch the VI dynamically using the VI Server functions. You can abort the VI in several ways, such as a global variable (sloppy, but works), queue, or even using the "Abort VI" method. Check the examples on how to load and run VIs dynamically. You will also find examples in this forum.


Message Edited by smercurio_fc on 05-20-2008 05:41 PM
Message 2 of 9
(3,579 Views)
how about posting your vi and see if someone can help you?
Best regards,
Krispiekream
0 Kudos
Message 3 of 9
(3,571 Views)
Thank you for the quick reply.
I attached a version of the program only with the relevant information.

smercurio-fc - I tried your first suggestion but encountered a similar problem. Lets say I put the vi in a secondary while loop that is running in parallel to the main loop. Once I start the vi I have no control over that loop leaving me no way to abort the vi.
I'm not sure I understand your second suggestion."Put the while loop code into a wrapper VI and launch the VI dynamically using the VI Server functions". What exactly do you mean by wrapper VI and VI server functions?

Thanks again for your help,
Shafran
Download All
Message 4 of 9
(3,558 Views)
If you right click on your Raster Selection event and select Edit Events Handled by this Case, at the bottom you will see a checkbox called 'Lock front panel until the event case for this event completes'. Uncheck it. You then have to determine how to pass a value from the main to the subVI while it is running. There are several ways - queues, shared variable, global variable, etc. Your example VI has no means to stop it but I would assume that in your actual VI you do have a stop button. In that event you could set a global variable that the subVI monitors inside it's while loop. Or it with the rest of your stop conditions in the subVI.
Message 5 of 9
(3,555 Views)
The VI Server allows you to load VIs dynamically and controls the programmatically. If you open the Example Finder (Help -> Find Examples) and search for "dynamic" you will see an example called "Dynamic Load Example", which shows one way of loading VIs dynamically. Other examples exist on the NI site:

Programmatically Opening and Running One VI from Another Using VI Server
Display a Splash Screen in LabVIEW using VI Server
Get/Set Control Values using VI Server in LabVIEW

The VI Server allows you to launch a VI and then return to your program. As with the separate loop, you would need a mechanism to stop the VI. As noted, you can use global variables, shared variables, or queues. With the VI Server you can simply call the "Abort VI" method:

Note that this is the same as pressing the red stop button on the toolbar, and is not a recommended way of stopping a VI since the VI will literally just stop, and depending on what the VI is doing that may be a bad thing.


Message Edited by smercurio_fc on 05-21-2008 09:34 AM
Message 6 of 9
(3,528 Views)
I loaded the VI dynamically and that seemed like just the thing I needed.
What would be the easiest way to dynamically load the VI with some variables from the main VI?
I know you guys suggested to use
global variables, shared variables, or queues. Assuming that I never used any of those 3 options which one would be the easiest to use?

Thanks again for all your help,
Shafran
Message 7 of 9
(3,512 Views)

What would be the easiest way to dynamically load the VI with some variables from the main VI?

We can answer that once "some variables from the main VI" is better defined.


I know you guys suggested to use global variables, shared variables, or queues. Assuming that I never used any of those 3 options which one would be the easiest to use?

Global variables are the easiest to use, and as with all things, they come with caveats. They can cause race conditions. An alternative is the "functional global" or "Action Engine". You can find examples of using globals in the Example Finder (Help -> Find Examples). Search for "global".

Shared variables are similar. They can be thought of as globals on steroids. They can be used to share data between VIs running on two different computers.

Queues are relatively easy to use and quite robust. Again, there are examples in the Example Finder.
Message 8 of 9
(3,504 Views)
"some variables from the main VI" - the synch program I attached "needs" to know the rate, number of samples and a pattern cluster. These might change depending on what I'm trying to measure. It also gives out a data array which I need to use in the main program. I put all of these variables in a cluster and defined it as a global variable. It seems to work for now (I might need to run it more to see if there are any problems).
I will look more into the shared variables and
queues, it might be helpfull (if not for this program then in the future).

Thank again for all the help,
Shafran

Message 9 of 9
(3,495 Views)