This snippet shows how to use Mutex to prevent multiple copies of your software from running. For a WinForms application, this code goes into the Program.cs file.

The magic starts in line 14 where we attempt to create a Mutex. A Mutex is a form of a lock, but it has the ability to cross applications, not just locking within an application. We take advantage of this by creating one with a special name for our application (in this case "mydomain.com myprogramname"). You need to make sure this is unique to your software, so be as specific as you can. The 'false' portion tells the system that we don't claim ownership of the Mutex.

Next we call WaitOne on the Mutex with two parameters. The first is the number of milliseconds we are willing to wait to claim the 'lock' on this Mutex. In this case we set it to 3 seconds in case another application is in the process of shutting down so we give it some time to do so. The second parameter tells the system if we want continue before we know if we have the Mutex or not. If this method returns true, then we have the Mutex and can proceed with our application. Otherwise we pop up a MessageBox letting the user know that another copy is already running.

All of this is wrapped in a using {} clause. The reason for this is we want to make sure that we release the Mutex when the application ends, otherwise you won't be able to run it again until you reboot your machine! The using ensures that the Dispose method is called on our Mutex so that it releases the value.

Comments
Yet another pearl from the snippet master!
excellent explanation of a very cool feature
using System;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            using (Mutex myLock = new Mutex(false, "mydomain.com myprogramname")) {
                if (myLock.WaitOne(3000, false)) {
                    Application.Run(new Form1());
                } else {
                    MessageBox.Show("Program already running", "There can be only one", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
    }
}

Awesome Code Here is a slight touch up you might like

using System;
using System.Threading;
using System.Windows.Forms;

namespace Chico
{
    public class SingleApp
    {
        private static Boolean IsSingleAppMode;
        private static Mutex singleApp;
        private const Single waitTime = 3000;

        public static Boolean Initialize(Boolean value, Form form)
        {
            try
            {
                if (value)
                {
                    using (singleApp = InitializeSingleAppMode(IsSingleAppMode))
                    {
                        if (StartAsSingleAppMode(IsSingleAppMode))
                        {
                            StartApplication(form);
                        }
                    }
                }
                else
                {
                    StartApplication(form);
                }
            }
            catch { }
            return value;
        }

        private static Boolean StartAsSingleAppMode(Boolean result)
        {
            return singleApp.WaitOne((UInt16)waitTime, result);
        }

        private static void StartApplication(Form mainForm)
        {          
            Application.Run(mainForm);
        }

        private static Mutex InitializeSingleAppMode(Boolean result)
        {
            return new Mutex(result, "anydomain.com myprogramname", out result);
        }
    }
}

Edited 5 Years Ago by CsharpChico: n/a