Using a mutex to prevent multiple copies of an application from running…(C#)

EDIT: Please read the comments as well, as they make some good suggestions.

I learned a few things yesterday, which I think are worth sharing. Perhaps eventually I will write this up as an article, rather than just a post.

Anyway, with Todo 3.0 I don’t want users having more than copy of Todo open at one time. If a user makes changes in one instance of the program, and it saves it, then the user makes changes in the other instance of Todo, the changes made in the second instance will overwrite any changes made in the first instance. The end result is a confused user who is wondering why tasks on thier todo list weren’t saved.

The way to go about fixing this is to prevent the user from opening more than one copy of Todo. There are a couple ways this could be done – you could open up a file when you first start your program, and keep it open until you close your program. If, when you start up your program, you aren’t able to open the file because it is already opened by another program, you let the user know that there is another instance of your program running. For the most part, this does work. So why not use it? Well, having a file open is a waste of resources. That, and there is a better way.

In the world of multi-threaded programming there is something called a mutex. It’s basically restricts access to a particular part of your program to a single thread, and forces any other threads to wait until that part of the program is complete. This comes in handy for synching things between threads. This can also be used to ensure that only one instance of your program is running. Here’s some code snippets straight from Todo 3.0:

In your main C# form, declare a mutex:

private Mutex mutex;
private string mutexName = "Todo3.0Mutex";

Then, in your form constructor, you want to do the following:

//before anything else happens, check for an existing mutex with the same name. If there is a mutex
//with the same name, that means there is already one copy of Todo 3.0 open, so let the user know
//what is going on, and kill this one
try
{
mutex = Mutex.OpenExisting(mutexName);
//since it hasn't thrown an exception, then we already have one copy of the app open.
MessageBox.Show("A copy of Todo 3.0 is already open. Please check your system tray (notification area).",
"Todo 3.0", MessageBoxButtons.OK, MessageBoxIcon.Information);
Environment.Exit(0);
}
catch (Exception Ex)
{
//since we didn't find a mutex with that name, create one
Debug.WriteLine("Exception thrown:" + Ex.Message + " Creating a new mutex...");
mutex = new Mutex(true, mutexName);
}

So, now you have a named mutex, and you have ensured that only one instance of your program is open. There is only one thing left to do – when you exit your program you need to be sure to release the mutex:

mutex.ReleaseMutex();

Now you may have noticed something strange – in the constructor, after I check for a named mutex, and I find one, I let the user know that there is already one instance of the progam open, then I call:

Environment.Exit(0);

Why do I do this? Because of the structure of how the Windows Forms messages are handled, if I were to call Application.Exit(), it wouldn’t work. Application.Run() needs to be called first in order for Application.Exit() to work, which hasn’t happened yet because we are only in the constructor. There is probably a better/safer way to do this, but I haven’t figured it out yet and this seems to be working fine in the mean time.

Technically, rather than using mutexes (or mutices? What is the plural of mutex?), I could have used Semaphores. A Semaphore is similar to a mutex, but rather than restricting access to only a single thread, it can set a limit to the number of threads that can access a resource. For example, we could limit access to a particular resource to 3 threads. In our case, we only need one thread to access things, so it’s probably a bit more kosher to use a mutex.

If I am completely wrong about something in this post, let me know. My understanding of multi-threaded programming is very limited. Either way, I thought this was pretty cool, and I figured some of you other folks out there might be interested in seeing this.

(Sorry for the borked code – it should be nicely tabbed, but the code tags don’t seem to like that. Oh well).

Advertisements

9 comments

  1. kamran

    it is really helpful, days ago i developed window application and i was facing the problem, if a user clicks on a form every time a new instance was created. that was not the thing i want. but this post really help me a lot.
    thanx

  2. Ben

    I’m late to the party here I know, but here’s my 2 cents…

    My solution to this problem is to create a Mutex object with the Assembly name. The constructor for Mutex provides an out bool that tells us if the Mutex was already existing or not. This saves having to remember to release it at the end of the application. I guess this may not work in all applications, but it’s always done the job for me.

    bool createdNew;
    Mutex m = new Mutex( true, System.Reflection.Assembly.GetExecutingAssembly().FullName, out createdNew );
    if( !createdNew )
    {
    // Instance already running; exit.
    log.Error( “Exiting: Instance already running” );
    return;
    }

  3. Julien

    This code is potentially flawed. Consider for example that a lengthy operation takes place after you ckeck the mutex existence and before you create the mutex:

    try
    {
    mutex = Mutex.OpenExisting(mutexName);
    MessageBox.Show(“…”, “”, MessageBoxButtons.OK, MessageBoxIcon.Information);
    Environment.Exit(0);
    }
    catch (Exception Ex)
    {
    Thread.Sleep(2000); // HERE!
    mutex = new Mutex(true, mutexName);
    }

    This would allow for a second program to pass the check while a first program is running but has not created his mutex just yet. This is a common flawed pattern in multithreading programming infortunately.

    For this to work properly the check and creation of the mutex must happen atomically in one step, which you can achieve with the constructor Mutex(bool, string, out bool)

    -julien

  4. Joel

    Julien: Good call. I’m still learning lots about multi-threaded programming, so I am by no means an expert. Thanks for pointing that out!

  5. logicPwn

    How would that solve anything Julien. 2 more seconds that it could pass a mutex on a different core. Haha that makes it worse. My code

    try
    {
    appMutex = Mutex.OpenExisting(“chromium-updater-v2”);
    MessageBox.Show(“Only one instance of Chromium Updater may exist.”);
    Environment.Exit(0);
    }
    catch (WaitHandleCannotBeOpenedException)
    {
    appMutex = new Mutex(false, “chromium-updater-v2”);
    }

    put it in main func

  6. anonymous

    @logicPwn is an idiot who cannot read. The thread sleep is SIMULATING a long running process. Julien is correct.

    bool isOwned;

    _mutex = new Mutex(true, _mutexName, out isOwned);

    if (!isOwned)
    {
    Environment.Exit(0);
    return;
    }

    base.OnStartup(e); // carry on starting up

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s