Just a quick post to show how to restrict the number of instances of your application running.

I’ve done this one in WPF, but it should apply to all types of .Net applications.

The main class is:

    public static class InstanceCheck
    {
        private static Mutex[] mutexes;

        public static bool CheckMaxInstances(int maxInstances)
        {
            mutexes = new Mutex[maxInstances];
            int i = 0;
            for (; i < maxInstances; ++i)
            {
                mutexes[i] = new Mutex(false, string.Format("myAppName{0}", i));
                if (mutexes[i].WaitOne(0, false))
                {
                    break;
                }
            }

            return i < maxInstances;
        }
    }

You can download the code from here