10-26-2023 10:53 AM
Help a (relative) novice out here please. Long story short, I have a simple test program (attached) to figure out how to shut down an application programmatically when the user presses the Windows ‘X’. The problem is, the loop doesn’t iterate. I assume I’m doing something wrong with the Event structure, but due to old age or simple lack of knowledge, I can’t figure it out. Can any of you geniuses put me on the right track?
Solved! Go to Solution.
10-26-2023 11:16 AM
Event structures only run when one of their frames is selected to run. Until then the code waits on them. Loops don't run again until everything inside of them has completed, and because you don't have any other event frames, literally the only way for the loop to run is for you to initiate the panel close, triggering the one and only frame.
As an immediate short solution, you could just add another frame, set it to the Timeout event, and set a timeout. Probably 500 ms to match your loop timer. This would run that frame after 500 ms of inactivity. That will work in the short run, but if you need to add any more events it could stop your whole loop from iterating.
In the long term it's best to have one While loop running just the event structure and nothing else, and have that loop send a signal to the main loop to stop when your program needs to end, or make any other changes to the state of the main loop. That way your process and your event handling aren't able to lock each other up.
It might help to look at a post I made a while back about event structures:
https://forums.ni.com/t5/LabVIEW/Struggling-with-Event-Structures/m-p/4086346#M1176175
10-26-2023 12:05 PM - edited 10-26-2023 12:10 PM
Great advice, thank you. Thanks also for the link to your post on Event Structures. Very informative.
10-26-2023 12:07 PM - edited 10-26-2023 12:08 PM
Yes, as suggested add a 500 ms timeout case to replace your 500ms wait.
There are several other issues that you might want to address:
10-26-2023 12:19 PM
I appreciate the tips. Thank you.
10-26-2023 12:26 PM
Any reason to not just take the windows X out of the equation?
10-26-2023 12:34 PM
That's certainly an option. Currently, my production program, (what I shared was just me playing in my sandbox) has both the 'X' and a 'Stop' button. Pressing Stop provides a controlled shutdown, whereas 'X' ... well you know.
Deleting X would force users to press Stop, if they remembered. Thing is, people are so set in the Windows paradigm that I feel removing X would cause a minor panic. To avoid this I'm going to send the program to it's End case for a controlled shutdown when X is pressed, and probably remove the Stop button instead.
10-26-2023 12:51 PM - edited 10-26-2023 12:52 PM
Pressing [X] is the correct way to stop an application and you can definitely discard it and trigger any required shutdown steps. Every computer user is familiar with the [X], no point for other mechanisms! (See also slide 27+ in my old presentation)
Here's a slightly cleaned up version of your code, maybe it can give you some ideas.
.
10-26-2023 01:02 PM
That's nice. I'll have to study it a little to be sure I really understand it, but thank you for taking the time to do this.