views:

461

answers:

5

Hi,

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,

+1  A: 

http://www.codeproject.com/KB/cs/CSSIApp.aspx

Alex Black
Really, that is a VB solution...
Ian
+7  A: 

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

See this or this or this.

Anton Gogolev
A: 

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.

ckittel
+2  A: 

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);
            }
        }
    }
lance
The code display seems to be thrown off by the @"Global\" -- my apologies.
lance
+1  A: 

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

nWorx