views:

115

answers:

4

My application needs to behave slightly differently when it loads if there is already an instance running.

I understand how to use a mutex to prevent additional instances loading, but that doesn't quite solve my problem.

For example:

  • Instance 1 loads, gets the mutex.
  • Instance 2 loads, can't get the mutex, knows there's another instance. So far, so good.
  • Instance 1 closes, releases the mutex.
  • Instance 3 loads, gets the mutex, doesn't know that Instance 2 is still running.

Any ideas? Thankfully it doesn't need to deal with multiple user accounts or anything like that.

(C#, desktop application)

Edit: To clarify, the application doesn't need to be restricted to a single instance, just perform a slightly different start-up action if there's another instance already running. Multiple instances are fine (and expected).

+1  A: 

Try using a Semaphore instead of a Mutex

Panagiotis Kanavos
+2  A: 

Another approach is to detect the running instance as detailed in Scott Hanselman's blog

His example activates the first instance when the second tries.

However, it wouldn't be hard to get the second instance to just stop if that's what you wanted.

ChrisF
Why won't anybody use the existing mechanism in [WindowsFormsApplicationBase](http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.aspx)? Just because its namespace contains VisualBasic! If the authors had put it in a namespace Foo it would be used by much more people.
Oliver
+4  A: 

This will probably do just what you want. It has the nice additional feature of bringing the already running instance forward.

EDIT: updated the code to determine the application title automatically.

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

static void Main()
{
    if (!EnsureSingleInstance())
    {
        return;
    }

    //...
}

static bool EnsureSingleInstance()
{
    Process currentProcess = Process.GetCurrentProcess();

    string applicationTitle = ((AssemblyTitleAttribute)Attribute
        .GetCustomAttribute(Assembly.GetExecutingAssembly(),
        typeof(AssemblyTitleAttribute), false)).Title;

    var runningProcess = (from process in Process.GetProcesses()
                          where
                            process.Id != currentProcess.Id &&
                            process.ProcessName.Equals(
                              currentProcess.ProcessName,
                              StringComparison.Ordinal)
                          select process).FirstOrDefault();

    if (runningProcess != null)
    {
        ShowWindow(runningProcess.MainWindowHandle, SW_SHOWMAXIMIZED);
        SetForegroundWindow(runningProcess.MainWindowHandle);

        return false;
    }

    return true;
}

[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

private const int SW_SHOWMAXIMIZED = 3;
Sandor Drieënhuizen
I'd add that APPLICATION-TITLE is the executable filename without the extension (if I remember correctly).
Tamás Szelei
@sztomi: thanks for sparking the memory of having code lying around for determining that automatically.
Sandor Drieënhuizen
Haha, nice. I think I hardcoded it when I had this problem. This is much better :)
Tamás Szelei
This looks promising, I'll give it a try, thanks.
Andy
Yep, that did it nicely. Perfect.
Andy
A: 

Could you simply check GetLastError() after creating the mutex with CreateMutex()? If it returns ERROR_ALREADY_EXISTS, then there is another running instance of your application.

According to http://msdn.microsoft.com/en-us/library/ms682411%28VS.85%29.aspx,

If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object, GetLastError returns ERROR_ALREADY_EXISTS, bInitialOwner is ignored, and the calling thread is not granted ownership. However, if the caller has limited access rights, the function will fail with ERROR_ACCESS_DENIED and the caller should use the OpenMutex function.

EDIT: Just realized this was a C#/.Net question, sorry.

In .Net, use the Mutex constructor that returns the createdNew flag, http://msdn.microsoft.com/en-us/library/bwe34f1k%28VS.80%29.aspx:

public Mutex (
    bool initiallyOwned,
    string name,
    out bool createdNew
)
GBegen