Reputation: 1873
I am completely new to Mutex.
I dont understand what this means:
WaitOne(TimeSpan) Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval. (Inherited from WaitHandle.)
E.g. if I use:
static void Main()
{
using(Mutex mutex = new Mutex(false, appGuid))
{
if(!mutex.WaitOne(2000, false))
{
MessageBox.Show("Instance already running");
return;
}
GC.Collect();
Application.Run(new Form1());
}
}
does it mean that once the line
if(!mutex.WaitOne(2000, false))
is fired, it waits 2 seconds before it checks if there is a lock on the thread ?
Upvotes: 1
Views: 2089
Reputation: 3589
This means that the current thread will block either until someone calls mutex.ReleaseMutex()
or the 2000ms timeout has passed. If the timeout is reached, the operation returns false
.
More details on the method call are available in this MSDN link.
So the bottom line is it doesn't matter what value you pass as a timeout, a call to mutex.ReleaseMutex()
will immediately release the thread anyway - the timeout is there so that the call does not wait infinitely in case the mutex is never release or released at a period considered late by the application's circumstances.
Upvotes: 3