#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.



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();

            // Run application (this call blocks)

            return false;

        // Called when user tries starting another instance
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs 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;

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

    public class Startup
        public static void Main(string[] args)
            MyApplicationBase appBase = new MyApplicationBase();

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