views:

210

answers:

1

I'm writing a system tray Winforms app which does some very important cleanup on Application.ApplicationExit. When I hit the stop button while debugging in Visual Studio, this the process exits, but this cleanup code doesn't get called. Is there some way I can make sure it gets called in this situation?

+2  A: 

No. When you hit the stop button you can expect your application to not execute anymore code (this allows you to exit infinite loops). If it executed more code, then the stop button would be a close button. The stop button allows you to kill your application without having to open taskman, which would likely kill VS also.

I would suggest having a close menu item on a context menu for the icon. When the user clicks it, your app would close & clean-up, allowing your program to exit gracefully.

If you want more information about the stop debugging button check out the MSDN reference on it.

Lucas McCoy
Yes I have a close context button. The real problem is that when the app fails to close gracefully, it leaves a partially mounted virtual disk active which makes Windows unstable.
mcintyre321
**@mcintyre321:** Why can't you just hit the close context button? It would seam easier then trying to turn the stop debugging button into something that it's not meant to be.
Lucas McCoy
sometimes it becomes a bit unresponsive!
mcintyre321