Close application when last form closes?

MrLucky

Freshman
Joined
Mar 5, 2006
Messages
47
Hi again,

I've got another quetion.

In Visual Basic .NET, On your project properties page, you had an option to chose when the application should exit (when main form closes or when last open form closes).

But I can't find this option in C#. How can I do this?
 
I believe in C# that an application automatically closes when the last form is closed. If however you wish the application to exit when a specific form is closed you could add Application.Exit to one of the close events for that form.
 
Generally, an application closes when the form on which the message loop is being run is closed. VB.Net has introduced a few features in version 8 to make it behave more like VB6, including the "Application Framework." Like VB6, this allows the message loop to be run outside the UI windows so that no one particular window must stay open for the program to continue to run. This feature is specific to VB. One possibility is to inherit the ApplicationContext class and pass an instance of your custom ApplicationContext to the Application.Run method.

Here is an example, but I've never done this before, and I'm not sure that it is 100% kosher. It appears to work, but I'm not sure if the way I've done it can cause threading issues.
C#:
public class MC: ApplicationContext{
	frmMain MainFrm = new frmMain();
	frmAbout AboutFrm = new frmAbout();

	public MC() {

		MainFrm.Closed += OnFormClosed;
		AboutFrm.Closed += OnFormClosed;

		MainFrm.Show();
		AboutFrm.Show();
	}

	void OnFormClosed(object sender, EventArgs e) {
		// The form that triggers the event will not be disposed yet, so we check each
		// form to see if it is either closed (IsDisposed) or is closing (== sender)
		if((MainFrm.IsDisposed || MainFrm == sender)
			&& (AboutFrm.IsDisposed || AboutFrm == sender)) {

			// The last form must be disposed before we can end the application
			if(!((Form)sender).IsDisposed) {
				((Form)sender).Dispose();
			}
			this.ExitThreadCore();
		}
	}
}
 
Hmm.. Marble_eater is correct the application does terminate when the main form is closed irrelevant of whether there is another form open or not. I could have sworn this wasn't the case.

EDIT:- I have seen the ApplicationContext inherited in the manor you suggest (as part of a Singleton application). In the example I saw they used a Collection of Forms. This would perhaps be a better solution than declaring and checking each one individually. Upon closing a Form simply remove it from the list and if it discovers it is the last item in the list you can close the application.
 
Last edited:
Cags said:
I have seen the ApplicationContext inherited in the manor you suggest (as part of a Singleton application). In the example I saw they used a Collection of Forms. This would perhaps be a better solution than declaring and checking each one individually. Upon closing a Form simply remove it from the list and if it discovers it is the last item in the list you can close the application.
The collection is definitely a better idea. You just need to make sure that new forms are added to the collection, otherwise you will run into serious problems when one form spawns another which doesn't end up being closed before the main thread terminates.
 
Back
Top