Question

Okay, so i've created my c# application, created an installer for it and have it working installed on my machine.

The problem is, when the user opens the application exe twice, there will be two instances of the application running. I only ever want one instance of the application to be running at any time, how do I go about doing this?

Thanks for your help,

Was it helpful?

Solution

The common technique for this is to create a named Mutex and check for its presence on application start.

See this or this.

Code from DDJ:

class App : Form
{
    Mutex mutex;

    App()
    {
        Text = "Single Instance!";
        mutex = new Mutex(false, "SINGLE_INSTANCE_MUTEX");
        if (!mutex.WaitOne(0, false)) 
        {
            mutex.Close();
            mutex = null;
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
            mutex.ReleaseMutex();
        base.Dispose(disposing);
    }

    static void Main()
    {
        App app = new App();
        if (app.mutex != null) Application.Run(app);
        else MessageBox.Show("Instance already running");
    }
}

OTHER TIPS

i solved this problem by this

[STAThread]
 static void Main()
    {

        Process[] result = Process.GetProcessesByName("ApplicationName");
        if (result.Length > 1)
        {
            MessageBox.Show("There is already a instance running.", "Information");
            System.Environment.Exit(0);
        }
        // here normal start 
    }

it is simple, but i had hardly time to check for better solutions.

With thanks to Messrs. Allen and Powell:

    static void Main() 
    {
        using (Mutex mutex = new Mutex(false, @"Global\" + appGuid)) {
            if (!mutex.WaitOne(0, false)) {
                string processName = GetProcessName();
                BringOldInstanceToFront(processName);
            }
            else {
                GC.Collect();
                Application.Run(new Voting());
            }
        }
    }

    private static void BringOldInstanceToFront(string processName) {
        Process[] RunningProcesses = Process.GetProcessesByName(processName);
        if (RunningProcesses.Length > 0) {
            Process runningProcess = RunningProcesses[0];
            if (runningProcess != null) {
                IntPtr mainWindowHandle = runningProcess.MainWindowHandle;
                NativeMethods.ShowWindowAsync(mainWindowHandle, (int) WindowConstants.ShowWindowConstants.SW_SHOWMINIMIZED);
            NativeMethods.ShowWindowAsync(mainWindowHandle, (int) WindowConstants.ShowWindowConstants.SW_RESTORE);
            }
        }
    }

I don't know the environment that you are operating in, but something to keep in mind about 'single-instance applications' is how you define single-instance. If the application can be run on multiple workstations at the same time, using a common datasource, is that an issue? Likewise, what about a terminal-services situation (or a "run as" situation) where more than one user is logged into the same computer, do you want to restrict the application in such a way that only one instance per-user, per-computer? Or are you okay with it simply being one instance per user?

The answer to these might lead you in one direction over another. For example, we have a 'single-instance' application with the scope being a group of computers. Only one user is allowed on within that group of workstations. We managed this by have a table in our shared data-source that tracked currently connected users. This is a maintenance issue as you need to be sure that table is 100% accurate all the time. Handling things like unexpected power outages on the workstation, leaving "bogus" records in that table took some careful handling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top