How do I gracefully exit WinForms app when hitting stop debugging in VS?

拥有回忆 提交于 2019-12-02 02:12:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/1228917/how-do-i-gracefully-exit-winforms-app-when-hitting-stop-debugging-in-vs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!