07-17-2017 01:32 PM
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.
Solved! Go to Solution.
07-17-2017 02:01 PM
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).
07-17-2017 02:13 PM
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.
07-17-2017 02:28 PM
Try something like this:
07-17-2017 02:32 PM
@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.
07-17-2017 03:41 PM
That sounds like what I'm looking for. Where do you find that?
07-17-2017 03:43 PM - edited 07-17-2017 03:56 PM
07-17-2017 03:53 PM - edited 07-17-2017 03:59 PM
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
07-17-2017 04:09 PM
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.
07-17-2017 04:55 PM
Thank you all. I got it to work using the close panel event like suggested.