How to restrict a program to a single instance

c#

Solution

I would use a Mutex

  static void Main()
  {
     string mutex_id = "MY_APP";
     using (Mutex mutex = new Mutex(false, mutex_id))
     {
        if (!mutex.WaitOne(0, false))
        {
           MessageBox.Show("Instance Already Running!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
           return;
        }
        // Do stuff
     }
  }

Problem

I have a console application in C# and I want to restrict my application to run only one instance at a time. How do I achieve this in C#?

Original source

Related problems