Reputation: 148524
class OneAtATimePlease
{
static void Main()
{
using (var mutex = new Mutex(false, "oreilly.com OneAtATimeDemo"))
{
if (mutex.WaitOne(TimeSpan.FromSeconds(3), false))
RunProgram();
else
{
Console.WriteLine("Another instance of the app is running. Bye!");
return;
}
}
}
static void RunProgram()
{
Console.WriteLine("Running. Press Enter to exit");
Console.ReadLine();
}
}
but those lines waits for someone to call Set()
function :
if (mutex.WaitOne(TimeSpan.FromSeconds(3), false))
RunProgram();
who is calling set
here ? this thread will never be released...(or will?)
what am i missing ?
Upvotes: 0
Views: 200
Reputation: 1806
WaitOne() immediately returns with a true if someone's not running it. Else, it blocks! The program which then finishes, causes one of the waiting programs to return with a true immediately.
Upvotes: 1