#1,106 – Creating a Single Instance Application

By default, when you build an application, the user can run multiple copies of the application.  Each one runs independently, managing its own window(s).  There are times, however, when you might want to enforce that no more than one instance of your application runs at a time.  You can do this using the Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase class.

You start by adding a reference to Microsoft.VisualBasic.dll.

1106-01

1106-02

You next create a class that derives from WindowsFormsApplicationBase and does the following:

  • Sets IsSingleInstance property to true (in constructor)
  • Overrides OnStartup method and creates an instance of your main WPF application class
    • (For first startup)
  • Overrides OnStartupNextInstance and reactivates the main window
    • (For subsequent startups)
    public class MyApplicationBase : WindowsFormsApplicationBase
    {
        private App myApp;

        public MyApplicationBase()
        {
            this.IsSingleInstance = true;
        }

        // Called when user starts the first instance of the app
        protected override bool OnStartup(StartupEventArgs eventArgs)
        {
            myApp = new App();
            myApp.InitializeComponent();

            // Run application (this call blocks)
            myApp.Run();

            return false;
        }

        // Called when user tries starting another instance
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);

            // App already running, so just bring window to front
            if (myApp.MainWindow.WindowState == System.Windows.WindowState.Minimized)
                myApp.MainWindow.WindowState = System.Windows.WindowState.Normal;
            myApp.MainWindow.Activate();
        }
    }

Next, you need to create a new Main method, where you’ll instantiate the new class.

    public class Startup
    {
        [STAThread]
        public static void Main(string[] args)
        {
            MyApplicationBase appBase = new MyApplicationBase();
            appBase.Run(args);
        }
    }

Finally, you’ll configure the project properties so that this new Startup.Main method is the startup object for your application.
1106-03

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #1,106 – Creating a Single Instance Application

  1. Pingback: Dew Drop – July 2, 2014 (#1807) | Morning Dew

  2. Arul M says:

    HI Sean,
    Excellent and its working like charm when our application has single window, instead if we have navigated to some other window we have to add the following line in Window_loaded method in the 2nd window.

    Application.Current.MainWindow = this;

    Otherwise it will throw an object reference null exception.

    Regards
    Arul Manivannan

  3. Progm says:

    Using this method I can not access resources declareted in Application.xaml. Using VB.NET & Visual studio 2015 enterprise & .NET framework 4. Application class inside Application.xaml.vb file does not have InitializeComponent (so it is unavailable) method but this method is in autogenerated Application.g.i.vb file.

Leave a comment