I have an application (I didn't write this) that is using Mutex like this:
static void Main(string[] args)
{
Mutex mutex = null;
try
{
mutex = Mutex.OpenExisting("SINGLEINSTANCE");
if (mutex != null)
{
return;
}
}
catch (WaitHandleCannotBeOpenedException ex)
{
mutex = new Mutex(true, "SINGLEINSTANCE");
}
// critical section here
}
But I know that the correct way is this:
private readonly Mutex m = new Mutex("SINGLEINSTANCE");
static void Main(string[] args) {
m.WaitOne();
try {
/* critical code */
}
finally {
m.ReleaseMutex();
}
}
This is used because only one process of this application can run at the same time. It is a console application that do some asynchronous job for a web application.
This code is in production, I don't wanna change it, unless there are some big problem with this code... Are there?