Single Instance App on Whidbey

In prior versions of .NET, there were lots of discussions about how to support single-instance WinForms applications. Here are a couple of links discussed some ways to do that: here and here and here's one in C++. For Whidbey, some of that functionality has been built into the Visual Basic libararies. But, the WindowsFormsApplicationBase and related classes are also accessible from C#.

First, to gain access to the WindowsFormsApplicationBase class in your C# project, you'll need to add the Microsoft.VisualBasic.ApplicationServices namespace to your .cs files. And, you'll need to add a reference in your project to the  Micorosft.VisualBase.dll file. At this point, you have access to this class in your code.

Then, I defined a SingleInstanceApplication class that derives from WindowsFormsApplicationBase. This class ensures that only one application of this type is ever run. If additional instances attempt to start, the Run method does not launch another main form, instead it fires a StartupNextInstance event back to your first process and exits without launching a new instance of your app.

 /// <summary>

 /// This class ensures that only a single instance of this application is

 /// ever created.

 /// </summary>

 public class SingleInstanceApplication : WindowsFormsApplicationBase

 {

       /// <summary>

       /// Constructor that intializes the authentication mode for this app.

       /// </summary>

       /// <param name="mode">Mode in which to run app.</param>

       public SingleInstanceApplication(AuthenticationMode mode)

             : base(mode)

       {

             InitializeAppProperties();

       }

       /// <summary>

       /// Default constructor.

       /// </summary>

       public SingleInstanceApplication()

       {

             InitializeAppProperties();

       }

       /// <summary>

       /// Initializes this application with the appropriate settings.

       /// </summary>

       protected virtual void InitializeAppProperties()

       {

             this.IsSingleInstance = true;

             this.EnableVisualStyles = true;

       }

       /// <summary>

       /// Runs the specified mainForm in this application context.

       /// </summary>

       /// <param name="mainForm">Form that is run.</param>

       public virtual void Run(Form mainForm)

       {

             // set up the main form.

             this.MainForm = mainForm;

             // then, run the the main form.

             this.Run(this.CommandLineArgs);

       }

       /// <summary>

       /// Runs this.MainForm in this application context. Converts the command

       /// line arguments correctly for the base this.Run method.

       /// </summary>

       /// <param name="commandLineArgs">Command line collection.</param>

       private void Run(ReadOnlyCollection<string> commandLineArgs)

       {

             // convert the Collection<string> to string[], so that it can be used

             // in the Run method.

             ArrayList list = new ArrayList(commandLineArgs);

             string[] commandLine = (string[])list.ToArray(typeof(string));

             this.Run(commandLine);

       }

}

 

Next, we need to replace the default code that's in the Main() function for your program. Typically, this code looks like:

static class Program

{

      /// <summary>

      /// The main entry point for the application.

      /// </summary>

      [STAThread]

      static void Main()

      {

            Application.EnableVisualStyles();

            Application.Run(new Form1());

      }

}

The updated code uses the SingleInstanceApplication class instead. It adds a listener for the StartupNextInstance event and calls the Run method on the class passing in an instance of the main form.

static class Program

{

      // private members

      private static Form mainForm;

      /// <summary>

      /// The main entry point for the application.

      /// </summary>

      [STAThread]

      static void Main()

      {

            // ensure only a single instance of this app runs.

            SingleInstanceApplication app = new SingleInstanceApplication();

            app.StartupNextInstance += new StartupNextInstanceEventHandler(OnAppStartupNextInstance);

                 

            mainForm = new d20RpgSuiteMainFrame();

            app.Run(mainForm);

      }

      /// <summary>

      /// Event handler for processing when the another application instance tries

      /// to startup. Bring the previous instance of the app to the front and

      /// process any command-line that's needed.

      /// </summary>

      /// <param name="sender">Object sending this message.</param>

      /// <param name="e">Event argument for this message.</param>

      static void OnAppStartupNextInstance(object sender, StartupNextInstanceEventArgs e)

      {

            // if the window is currently minimized, then restore it.

            if (mainForm.WindowState == FormWindowState.Minimized)

            {

                  mainForm.WindowState = FormWindowState.Normal;

            }

            // activate the current instance of the app, so that it's shown.

            mainForm.Activate();

            // todo: implement command-line processing as needed...

      }

}

As you can see from the code above, the OnAppStartupNextInstance event handler activates the first main form and ensures that it is restored from a minimized state. At this point, the first instance of the application will come up whenever a user tries to create a new instance. Also, you will probably need to respond to the command line of the new instance that's trying to be created. Those command line arguments are in the StartupNextInstanceEventArgs that is passed into the event handler. Many times new instance launching happens from the Windows Explorer when the user opens a file with an extension that is registered for your app. By responding to the command line, you can load the specified file in that single instance rather than always launching new instances.

This new class (WindowsFormsApplicationBase) really helps abstract away a lot of code that needed to be written in previous versions that dealt with named pipes between processes, memory mapped files, windows messages and more just for this single instance functionality. With Whidbey, using the SingleInstanceApplication class and adding an event handler in your Program class, it's pretty easy to do.