LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Terminating a Loop

Hello,
I have made a simple LabVIEW program with a main-while-loop that is running as
long as the program is running. The user interface consists of a start-button
and a stop-button (and other non-important controls). Inside the main-while-loop
there is a true/false-case-structure. The false-case is empty, and the true-case
runs whenever the "start"-button is pushed. The true-case calls a sub-vi which
takes a long time to execute (about ten minutes). I want to be able to stop the
entire program by pushing the stop-button in the user interface, without having
to wait for the sub-vi to finish execution.
What can I do to accomplish this?

I have wired the stop-button to the while loops termination-criteria-terminal,
but then the vi doesn't stop befo
re the sub-vi in the true-case has finished
execution.

Sorry if this is a trivial question, I have only used LabVIEW for a short time,
and I can't figure out any solutions to this problem myself.

-Marianne
0 Kudos
Message 1 of 7
(3,301 Views)
There are different ways how to do that. It's different if you run sub-vi dynamic or static. In case you run it static (usual method), use control reference. You need create reference of stop button in main vi and use it as input of sub.vi.

If you run sub-vi dynamically (by Run method of VI-server), use VI-server method: Set Control Value. It means that if you have stop button in sub.vi you can set it value from another vi by this method. This is will work only if you run sub.vi without option "Wait until done" of Run method.

I attached example of first (more usual approach) vi.
0 Kudos
Message 2 of 7
(3,301 Views)
Thanks for the answer.

But it doesn't quite work as I wanted it to. My main.vi also has a while-loop.
I added a while-loop to main.vi in your example and wired the stop-button also to the termination-symbol of this loop. Now the stop button only seems to restart the sub.vi, and occasionally stopping it.. What shall I do ?
0 Kudos
Message 4 of 7
(3,301 Views)
Get out sub.vi from while loop.
0 Kudos
Message 5 of 7
(3,301 Views)
Hi!

The solution to a user interface for "emergency stop" is the following.

First of all you need to have the main.vi and sub.vi running in different
threads. This
is an execution system option: Instead of "same as caller" you should
select for the sub.vi execution option "other2" or some name like that (I
don't have LV in this machine.)
Now that the main.vi and sub.vi run independently you should update the
emegency_stop global variable state to that of the button during each
circulation of the main.vi.
while loop. Remember to add some time delay (wait until next 30?
milliseconds multiple) in the main.vi while loop to leave proper
processor time for your sub.vi.
Then in your sub.vi place cleverly one or more emergency_stop read so
that your
sub.v
i responds (exits) fast enough to the pressing of the emergency_stop
button in the main.vi.
Now your sub.vi reacts on the stop button pressing in the main.vi.

So, the general solution to a control panel problem is multi-threading +
global variables.

One more thing, you should use local variables to initialize and to
reinitialize after pressing of the stop button. Otherwise the user can
initiate some double-click
based problems or some other exotic failures in the program stopping.
Also use error-in error-out interfaces to your sub-vi to make sure that
your
vi starts execute at the exactly right execution order like, first I do
this
then I do that and then I go to sub.vi

Best Regards,
Martti
Oulu, Finland

Mariannne wrote:

> Thanks for the answer.
>
> But it doesn't quite work as I wanted it to. My main.vi also has a
> while-loop.
> I added a while-loop to main.vi in your example and wired the
> stop-button also to the termination-symbol of this loop. Now the stop
> button only seem
s to restart the sub.vi, and occasionally stopping
> it.. What shall I do ?
0 Kudos
Message 6 of 7
(3,301 Views)
martti:

Dynamically launch your sub-vi. This is the way to do what you want
to do. When you dynamically launch, tell it not to wait for execution
to complete and tell it to auto close vi references. You can
communicate between the main vi and the dynamically launched vi using
queues, notifiers, occurrences, and lastly but least reliably globals.

You will need to make sure that you don't try to dynamically launch an
already running dynamically launched sub-vi or if it is acceptable you
will need to use VI templates (VITs) so that you can have multiple
simultaneously running dynamic sub-vis. To do this you could use an
acquire semaphore to prevent launching a VI until the already launched
VI releases that semaphore.

You can use a queue, notifier, etc. to tell the dynamically launched
VI to abort or vice versa for the dynamically launched VI to kill the
main program if you want or you can simply close the main VI from it's
front panel and the dynamically launched VI will continue on it's own
until it finishes.


Douglas De Clue
LabVIEW developer
ddeclue@bellsouth.net

martti wrote in message news:<3D353506.5D1D1181@spam.for.me>...
> Hi!
>
> The solution to a user interface for "emergency stop" is the following.
>
> First of all you need to have the main.vi and sub.vi running in different
> threads. This
> is an execution system option: Instead of "same as caller" you should
> select for the sub.vi execution option "other2" or some name like that (I
> don't have LV in this machine.)
> Now that the main.vi and sub.vi run independently you should update the
> emegency_stop global variable state to that of the button during each
> circulation of the main.vi.
> while loop. Remember to add some time delay (wait until next 30?
> milliseconds multiple) in the main.vi while loop to leave proper
> processor time for your sub.vi.
> Then in your sub.vi place cleverly one or more emergency_stop read so
> that your
> sub.vi responds (exits) fast enough to the pressing of the emergency_stop
> button in the main.vi.
> Now your sub.vi reacts on the stop button pressing in the main.vi.
>
> So, the general solution to a control panel problem is multi-threading +
> global variables.
>
> One more thing, you should use local variables to initialize and to
> reinitialize after pressing of the stop button. Otherwise the user can
> initiate some double-click
> based problems or some other exotic failures in the program stopping.
> Also use error-in error-out interfaces to your sub-vi to make sure that
> your
> vi starts execute at the exactly right execution order like, first I do
> this
> then I do that and then I go to sub.vi
>
> Best Regards,
> Martti
> Oulu, Finland
>
> Mariannne wrote:
>
> > Thanks for the answer.
> >
> > But it doesn't quite work as I wanted it to. My main.vi also has a
> > while-loop.
> > I added a while-loop to main.vi in your example and wired the
> > stop-button also to the termination-symbol of this loop. Now the stop
> > button only seems to restart the sub.vi, and occasionally stopping
> > it.. What shall I do ?
0 Kudos
Message 7 of 7
(3,301 Views)
hi,
what nadav has said is the more simpler approach. to add to his point if u need the sub vi as a dynamic one that approach would not work.
u can wire the stop button to global and check for the global in the sub vi. though use of globals should be avoided it is justified in this case.
remember that ur sub vi should keep on checking that to terminate the program
0 Kudos
Message 3 of 7
(3,301 Views)