Question on how to trap winform closing from VS designer
-
Is it possible to tell when a WinForm app is being closed from Visual Studio when a developer uses the "Stop Debugging" button? I'd like to perform some file cleanup, etc whenever the app shuts down, but when the app is closed from VS itself vs. the close button the form, the closing, dispose, etc is not called. Just wondering if someone has run into this and what they may have done. Thanks for any suggestions or comments.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Is it possible to tell when a WinForm app is being closed from Visual Studio when a developer uses the "Stop Debugging" button? I'd like to perform some file cleanup, etc whenever the app shuts down, but when the app is closed from VS itself vs. the close button the form, the closing, dispose, etc is not called. Just wondering if someone has run into this and what they may have done. Thanks for any suggestions or comments.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Form_Closing event is definitely called buddy...Check again..
-
Is it possible to tell when a WinForm app is being closed from Visual Studio when a developer uses the "Stop Debugging" button? I'd like to perform some file cleanup, etc whenever the app shuts down, but when the app is closed from VS itself vs. the close button the form, the closing, dispose, etc is not called. Just wondering if someone has run into this and what they may have done. Thanks for any suggestions or comments.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
All closing and exiting events will equally fire inside or outside Visual Studio. Use one of them and take advantage of this[^]. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Form_Closing event is definitely called buddy...Check again..
Closing is not called. When stopping the app with the "Stop Debugging" button in Visual Studio (take your flavor, 2005 or 2010) instead of closing the app through the form, the dispose, finalize and Form_closing events are bypassed. You can verify this by putting breakpoints on each and seeing if your app stops there.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
All closing and exiting events will equally fire inside or outside Visual Studio. Use one of them and take advantage of this[^]. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Hey Luc, I'm familiar with the Debugger.IsAttached because I have some debug.print statements that occur during various routines. I'm feeling rather blonde today. I created a new WinForm app from scratch in VS2010. Stuck this code in
Private Sub Form1\_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing MessageBox.Show("Look ma, I'm closing") End Sub
If I start the app and close it with the X in the caption bar, woot! If I start the app and close it with the Stop Debugging button in VS on the debugging toolbar, it's just not called? Am I totally overlooking something?
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Hey Luc, I'm familiar with the Debugger.IsAttached because I have some debug.print statements that occur during various routines. I'm feeling rather blonde today. I created a new WinForm app from scratch in VS2010. Stuck this code in
Private Sub Form1\_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing MessageBox.Show("Look ma, I'm closing") End Sub
If I start the app and close it with the X in the caption bar, woot! If I start the app and close it with the Stop Debugging button in VS on the debugging toolbar, it's just not called? Am I totally overlooking something?
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Nope. When you click on Stop Debugging, your code stops, period. There is no further exectuion of your application code. So, no, there is no way to do your file cleanup from inside your application. This would have to be done seperately, from some batch of script operation you launch yourself.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hey Luc, I'm familiar with the Debugger.IsAttached because I have some debug.print statements that occur during various routines. I'm feeling rather blonde today. I created a new WinForm app from scratch in VS2010. Stuck this code in
Private Sub Form1\_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing MessageBox.Show("Look ma, I'm closing") End Sub
If I start the app and close it with the X in the caption bar, woot! If I start the app and close it with the Stop Debugging button in VS on the debugging toolbar, it's just not called? Am I totally overlooking something?
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
I see no solution, stopping the debug session aborts the process, without generating any of the usual events (Form.FormClosing, Application.ApplicationExit, etc). :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Nope. When you click on Stop Debugging, your code stops, period. There is no further exectuion of your application code. So, no, there is no way to do your file cleanup from inside your application. This would have to be done seperately, from some batch of script operation you launch yourself.
A guide to posting questions on CodeProject[^]
Dave KreskowiakI think so. my 5
-
Is it possible to tell when a WinForm app is being closed from Visual Studio when a developer uses the "Stop Debugging" button? I'd like to perform some file cleanup, etc whenever the app shuts down, but when the app is closed from VS itself vs. the close button the form, the closing, dispose, etc is not called. Just wondering if someone has run into this and what they may have done. Thanks for any suggestions or comments.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Your question is clear & Dave is correct.
-
Nope. When you click on Stop Debugging, your code stops, period. There is no further exectuion of your application code. So, no, there is no way to do your file cleanup from inside your application. This would have to be done seperately, from some batch of script operation you launch yourself.
A guide to posting questions on CodeProject[^]
Dave KreskowiakThanks for the answers, just wanted to make sure I wasn't overlooking something. It would be nice to have a post debug (like the pre/post build events) events, but how often would you really use them. Really hate to rely on devs. having to run a bat file or something else, because it's only human to error, but that's the only real option here. Thanks again.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Is it possible to tell when a WinForm app is being closed from Visual Studio when a developer uses the "Stop Debugging" button? I'd like to perform some file cleanup, etc whenever the app shuts down, but when the app is closed from VS itself vs. the close button the form, the closing, dispose, etc is not called. Just wondering if someone has run into this and what they may have done. Thanks for any suggestions or comments.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
VB makes a distinction between
End
andStop
, two keyworks inherited from proto-BASIC.End
closes things down, fires events and cleans up memory;Stop
does exactly that: stops execution and leaves the application in a suspended state that can be inspected. I believe that "Stop Debugging" is effectively the same as theStop
statement, which means that execution just... stops. If you need to test clean up code while debugging your application, you should close the application in the same way your users will close it, such as the "Close Form" button on the main form, clicking a different button, using a menu item or the like. -
Thanks for the answers, just wanted to make sure I wasn't overlooking something. It would be nice to have a post debug (like the pre/post build events) events, but how often would you really use them. Really hate to rely on devs. having to run a bat file or something else, because it's only human to error, but that's the only real option here. Thanks again.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
I assume you are trying to get back to a start up state, is it possible to move the cleanup to the aplication start process?
Never underestimate the power of human stupidity RAH
-
I assume you are trying to get back to a start up state, is it possible to move the cleanup to the aplication start process?
Never underestimate the power of human stupidity RAH
I have that being done already (load and form_closing), but I wanted to cover the scenario in which a developer stopped the app via stop debugging button and immediately shuts VS after that. No biggie, I'll just make it known. Kinda a special case scenario.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Closing is not called. When stopping the app with the "Stop Debugging" button in Visual Studio (take your flavor, 2005 or 2010) instead of closing the app through the form, the dispose, finalize and Form_closing events are bypassed. You can verify this by putting breakpoints on each and seeing if your app stops there.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Let me verify it, I will get back to you..