Keep Windows Forms application alive till all forms were closed

Inside the forums I found some people asking how they could keep the application active when they open a new form and close the old one.

The main problem is, that the standard program.cs simply calls
Appliation.Run (new Form1());

As soon as this new Form1 is closed, the application will quit.

After thinking a little bit I just played around and had a small class which could be used to replace the call to Application.Run. It is just a small thing and I am not really sure that it is usefull.

To use this class just insert it into your solution. Maybe change the namespace so it fits your needs.

Afterwards you just replace the Application.Run call with the FormsApplication.Run call and whenever you open a new form you call FormsApplication.AddForm. Thats it!

(Copy & Paste the code so you can also read the lines that are to long)

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace FormsApplicationTest
{
    /// <summary>
    /// Manages open Forms and makes sure that the ApplicationContext of the main loop always has an open form till the last form is closed.
    /// </summary>
    /// <remarks>
    /// To use this class: Simply replace the Application.Run call in program.cs with FormsApplication.Run and make sure that you use AddForm on all opened forms.
    /// </remarks>
    public class FormsApplication
    {
        private static ApplicationContext _appContext;
        private static readonly List<Form> _forms = new List<Form>();

        /// <summary>
        /// Run the Application Loop
        /// </summary>
        public static void Run()
        {
            _appContext = new ApplicationContext();
            Application.Run(_appContext);
        }

        /// <summary>
        /// Run the Application Loop with a given context.
        /// </summary>
        public static void Run(ApplicationContext context)
        {
            _appContext = context;
            if (context.MainForm != null)
            {
                _forms.Add(context.MainForm);
                context.MainForm.Closed += ClosedEventHandler;
            }
            Application.Run(context);
        }

        /// <summary>
        /// Run the Application Loop with a given form.
        /// </summary>
        public static void Run(Form form)
        {
            _forms.Add(form);
            form.Closed += ClosedEventHandler;
            _appContext = new ApplicationContext();
            _appContext.MainForm = form;
            Application.Run(_appContext);
        }

        /// <summary>
        /// Event handler that removes a form when it is closed
        /// </summary>
        private static void ClosedEventHandler(object sender, EventArgs e)
        {
            var form = (Form)sender;
            _forms.Remove(form);
            if (_appContext.MainForm == form && _forms.Count > 0)
                _appContext.MainForm = _forms[0];
        }

        /// <summary>
        /// Add a form to the list of known forms.
        /// </summary>
        /// <param name="form">Form to add to the list.</param>
        public static void AddForm(Form form)
        {
            _forms.Add(form);
            form.Closed += ClosedEventHandler;
        }
    }
}

Feel free to send me a small note (konrad@neitzel.de) if you found this usefull.

Advertisements
This entry was posted in Software Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s