Typically I see these two pieces of code all around. Both works in my case too, but which should I stick to?
Case 1:
bool isNew = false;
Mutex mutex = new Mutex(true, "MyApp_Mutex", out isNew);
if (!isNew)
{
MessageBox.Show("already running.", "Multiple Instances Not Allowed",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
Case 2:
Mutex mutex = new Mutex(false, "MyApp_Mutex"))
if (!mutex.WaitOne(0, false))
{
MessageBox.Show("already running.", "Multiple Instances Not Allowed",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
Which is the ideal way between the two to prevent multiple instances?
What is the difference?
Moreover I see codes like these:
//if not return{ mutex.ReleaseMutex(); GC.Collect(); //application.Run(); GC.KeepAlive(mutex);
under the second method but never with the first. Why is that so? Or did I get that wrong?
Basically it lies with the proper understanding of the parameters and methods used. I would appreciate if someone can briefly detail it, I understand not half when reading msdn documentation..