Environment.Exit () is a more brutal way to close the application, but in general, if you need to kill the application to close it, I think that you are considering the problem wrong. You should better study why other threads are not closing?
You can look into the FormClosing event in the main form and close all resources that suspend the application, preventing it from closing.
This is how I found application specific resources.
- In debug mode, turn on threads (this will allow you to see all the threads that the application is running on)
- Close the application so that it is not properly closed.
- Click the pause button in Visual Studio
- Look at the list of topics and click on them to see where it is in the code that they hang, now that you can see which resources block your application from closing, go to the FormClosing event and close it / Dispose.
- Repeat until the application closes correctly :)
Keep in mind that the list of threads in debug mode will show some threads that are started, but not under your control, these threads have a name, and when you click on them, you get a message that you have no characters. They can be safely ignored.
One of the reasons that your application closes gracefully is because if some resources (say, FileStream ) do not work, therefore, using some api to make it fast, all kinds of βrandomβ problems, for example, files settings / data that are not recorded, etc.
EX May 25 '09 at 6:29 a.m. 2009-05-25 06:29
source share