#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 .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 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

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

%d bloggers like this: