Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Only One Copy Of Application

0.00/5 (No votes)
6 Nov 2011 1  
Yet another way to this is as follows: using System;using System.Collections.Generic;using System.Windows.Forms;using System.Threading;namespace OnlyOneInstance{ static class Program { [STAThread] static void Main() { bool...
Yet another way to this is as follows:

C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace OnlyOneInstance
{
    static class Program
    {
       
        [STAThread]
        static void Main()
        {
            bool instantiated;
          /* If instantiated is true, this is the first instance of the application; else, another instance is running. */
            Mutex mutex = new Mutex(true, "UniqueID", out instantiated);
            if (!instantiated)
            {
                MessageBox.Show("Already Running");
               return;
            }
            Application.Run(new Form1());
            GC.KeepAlive(mutex);
        }
    }
}


Take a look there[^] to read the complete article.

To download the sample code, you can go here[^]
After downloading, go to bin directory and then debug OnlyOneInstance.exe file twice, you will see a message box which states Program Already Running.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here