Could you please explain why this Mutex helping allow to run only 1 instance of application at a time doesn't work (C#)?

StackOverflow https://stackoverflow.com/questions/16669644

Question

Here is the article I read http://kristofverbiest.blogspot.com/2008/11/creating-single-instance-application.html, followed the steps by the article, I have the following code, I'm almost sure it can't work and it can't:

static class Program
{
    static Mutex mutex;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool alreadyRun;
        mutex = new Mutex(false, Guid.NewGuid().ToString().Replace("-", ""), out alreadyRun);
        if (!alreadyRun) return;                            
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());            
    }
}

The article says the Mutex name passed in can be random, the MyAppName is just for easy debugging (so I left it out), 'Local//' was also left out. I wonder how it can work? That constructor will succeed and turn alreadyRun true whenever the random GUID string is unique (I mean there hasn't been any Mutex with that name created before). That means, there is a very little chance for alreadyRun becoming false. In fact, I've tried the code above for many times and I could run many instances of my application as I liked.

After trying a fixed string like "unique" for the mutex name , it simply works. However this won't work if there is some another application creating another mutex with the same name. And I now ended up with this solution:

  • We can't use random string, we have to use fixed string and this string should be long and complicated, it is like your password, your id to be sure that there is little chance for any other application to use the same name. Here is an example that I'm sure to say, there is little chance for any other (even a machine) to think of: ilove.netilovejava1234forever56789thismyid_itisevenlongerlongerlonger_howlongcanitbe_maybe8000characters?_canitbeso?

Please explain about the article (looks like it is from a pro blogger) and give me any other solution using Mutex (I've been fed up with using GetProcessesByName, it's not good so far). Thanks!

Was it helpful?

Solution

Reading the article you sent you are not following it correctly:

"The name of the mutex should contain a GUID, which ensures that no other application uses the same name. If you don’t know how to create a GUID, click here."

Not a new GUID for each instance. Create a GUID once making the string unique.

You could use something like

static class Program
{
    static Mutex mutex;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        var guid = ((System.Runtime.InteropServices.GuidAttribute)(System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false).Single())).Value;

        bool alreadyRun;
        mutex = new Mutex(false, "App_Name" + guid.ToString(), out alreadyRun);
        if (!alreadyRun) return;        
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());        }
}

Then if you want another version of your app to run at the same time just give it a new GUID. You can prefix Local\ as per the article if required.

Here is an imageenter image description here

OTHER TIPS

You don't need a name to create a mutex, but you should have one if you intend to use it across process boundaries, e.g. to signal that an instance of the application is already running.

In your code, you're creating a different name each time the application starts. This name will not match the name of any other mutexes, so the code will pass on through.

Try setting the name to something distinct but definite.

The remarks on this MSDN page explain things well.

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