1

Possible duplicate: Run single instance of an application using Mutex

I am using VS 2008 in C# on a console application. Not sure if there is any class available (process?) to limit only single console application running at a time.

2
  • Did you mean to run just one instance of the program? Commented Jun 3, 2009 at 4:42
  • I'm pretty sure it's a dupe... Commented Jun 3, 2009 at 4:42

2 Answers 2

3

There is no such class, but you can use System.Threading.Mutex to build one.

Mutex is a synchronization primitive that can also be used for interprocess synchronization.

See example: How ensure that only one instance of an application will run?

Sign up to request clarification or add additional context in comments.

Comments

1

I quickly slapped this together, but it does show the second console briefly and then hides it. Not sure if there's a way to block the second console from even showing up...

class Program { private static Mutex mutex; static void Main(string[] args) { mutex = new Mutex(true, "MyMutex"); if (!mutex.WaitOne(0, false)) { return; } Console.WriteLine("Application started"); Console.ReadKey(true); } } 

You'll need to add a reference to System.Threading.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.