My application is growing quite fast, I'm up to about 15 different forms and 20 different classes (at least). However, I seem to be stuck trying to fully close the entire application.
Now, I know that inside the Program.cs, there is the line of
As far as I understand it, closing MainForm from anywhere results in the entire application unloading, calling every forms/classes closing/disposing events if necessary. However, what if I'm trying to close/unload the application in another way.
In Program.cs I hook the CurrentDomain_UnhandledException event. Inside of that, I log the error message inside a try block, and in a finally block, call
This seems to successfully close/unload the entire application and there are no lingering processes of my app running.
With that in mind, I don't display the MainForm right away, it doesn't show in the taskbar and is minimized on startup. I then show a new form, for logging into the application and handling other things for the user before restoring the MainForm. On the closing event of the Login form, all I do is call Application.Exit() and in every test it correctly closes/unloads the application and there are no lingering processes of my app running.
I have 1 more form that gets displayed on certain user settings upon logging in. The Login form is minimized and .Hide() is called on it while the main form is restored. On restoring the main form, if it needs to be, I show a Settings form right overtop the MainForm. This is all currently working exactly how I would like it to so far. On the Settings form though, I have an "Exit Program" button. Since you have to complete the entire Settings form to get into the main application, I figured some people might choose to save their progress/settings and continue later. Thus, if the "Exit Program" button is clicked, SQL saves the currently set settings and calls
However, it's at this point that the Settings form closes, the MainForm closes (remember it's restored and shown in taskbar) as I can see it go away, but I still have the a lingering process. If inside of the IDE, VS doesn't stop the debugging session as something is apparently running but not crashing the program...
I am more than curious as to why Application.Exit() is correctly working in 2 completely different areas of code, but yet another area of code, it only semi works?
Now, I know that inside the Program.cs, there is the line of
Code:
Application.Run(new MainForm()); //We use MainForm instead of Form1
In Program.cs I hook the CurrentDomain_UnhandledException event. Inside of that, I log the error message inside a try block, and in a finally block, call
Code:
Application.Exit()
With that in mind, I don't display the MainForm right away, it doesn't show in the taskbar and is minimized on startup. I then show a new form, for logging into the application and handling other things for the user before restoring the MainForm. On the closing event of the Login form, all I do is call Application.Exit() and in every test it correctly closes/unloads the application and there are no lingering processes of my app running.
I have 1 more form that gets displayed on certain user settings upon logging in. The Login form is minimized and .Hide() is called on it while the main form is restored. On restoring the main form, if it needs to be, I show a Settings form right overtop the MainForm. This is all currently working exactly how I would like it to so far. On the Settings form though, I have an "Exit Program" button. Since you have to complete the entire Settings form to get into the main application, I figured some people might choose to save their progress/settings and continue later. Thus, if the "Exit Program" button is clicked, SQL saves the currently set settings and calls
Code:
Application.Exit();
I am more than curious as to why Application.Exit() is correctly working in 2 completely different areas of code, but yet another area of code, it only semi works?