From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Close a SubVI from the main VI.

Solved!
Go to solution

   I have a main vi that calls a SubVI in which a front panel comes up for the user to use. The SubVI has a close button, but if someone closes it another way ("X" button at the top, ctl+w, or whatever) then the program is stuck, waiting for the SubVI to stop running with no way for the user to stop it.

   I've made it so the "X" button cannot be used by the user but in case there are other creative ways to close the window, I would like to have some code in the Main VI that ends the SubVI when the front panel is closed. You can use a VI property node to check the state of the SubVI front panel but I have not found any way to close a SubVI from the Main VI.

 

   Does anyone know if this is possible and how to do it? Thanks.

0 Kudos
Message 1 of 10
(5,289 Views)
Solution
Accepted by topic author MattManML

Use an event structure in the subvi to capture the Panel Close event.  This should fire during all of those creative ways that the panel can be closed.  If you use the Panel Close? event (note the question mark), the event will still be captured but it can be discarded so you can close the panel how you want (destroying any references, doing cleanup, etc). 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 2 of 10
(5,274 Views)

I actually refuse to put "Stop" or "Close" buttons on my front panels.  99% of users that I have worked with go for that red X to close the application.  As already stated, I use the Panel Close? filter event to detect the panel being closed.  I then discard it, do some cleanup, and then programmatically close the panel when complete.


GCentral
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
0 Kudos
Message 3 of 10
(5,267 Views)

Try something like this:

Close sub-vi ref.png

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 4 of 10
(5,257 Views)

 


@crossrulz wrote:

I actually refuse to put "Stop" or "Close" buttons on my front panels.  99% of users that I have worked with go for that red X to close the application.  As already stated, I use the Panel Close? filter event to detect the panel being closed.  I then discard it, do some cleanup, and then programmatically close the panel when complete.


I find this to be the same with my customers.  About a year and a half ago I stopped putting stop buttons on the panel.  I don't think I've ever heard a single complaint about it. 

0 Kudos
Message 5 of 10
(5,252 Views)

That sounds like what I'm looking for. Where do you find that?

0 Kudos
Message 6 of 10
(5,225 Views)

@aputman@

 

   That sounds like what I want. Where do you find the Panel Close? event?

0 Kudos
Message 7 of 10
(5,222 Views)

I just finished a program that has numerous sub-VIs running independently, with control "from above".  With a single exception (described below), the top-level VI never tries to close one of its sub-VIs -- instead, it "requests" the sub-VI to "close itself" (with the idea that if the sub-VI is written properly, it will "clean up its mess" before exiting).

 

There are numerous ways to have the Top Level VI send a signal to a particular sub-VI -- Queues, via a reference to the sub-VI, etc.  How does the Top Level VI know if/when the sub-VI has exited?  Again, it can check a reference to the sub-VI and get its execution state.  Another method that I used is to have the sub-VI send an "I'm Exiting" message back to its caller just before it exits.  

 

If All Goes Well (which it almost never does!), the Top Level tells the sub-VI to Exit, waits a reasonable amount of time to see if it has exited, and if not, can "force-close" it (by executing its Abort method).  This is the Single Exception mentioned above -- try (very hard) to design your code so this will never need to be executed.  Note that if you are using a Queue to communicate from Top Level to sub-VI, one such "message" from sub-VI to Top VI would be to have the sub-VI release the Queue as the last function it executes.  Top VI can do a Queue Status -- if the Queue has been released by the sub-VI, this will generate an Error (Note -- using an "Error" to send information is, shall we say, awkward ...).

 

Bob Schor

Message 8 of 10
(5,209 Views)

It's part of the event structure in the Structure palette.  You'll want to read up on event structures because it won't be as simple as dropping it on the diagram.  You'll need to consider how the event structure will affect the existing code, what architecture already exists in the code, if any (state machine, producer/consumer, etc), can the event structure be used to capture other events, etc.  

Here is an example of how this might be done.   

panel_close.png

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 9 of 10
(5,195 Views)

Thank you all. I got it to work using the close panel event like suggested.

0 Kudos
Message 10 of 10
(5,179 Views)