Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using waitone() method

Tags:

c#

static Mutex mutex = new Mutex (false, "oreilly.com OneAtATimeDemo");

static void Main()
{
    // Wait a few seconds if contended, in case another instance
    // of the program is still in the process of shutting down.

    if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))
    {
      Console.WriteLine ("Another instance of the app is running. Bye!");
      return;
    }

    try
    {
      Console.WriteLine ("Running. Press Enter to exit");
      Console.ReadLine();
    }
    finally { mutex.ReleaseMutex(); }
}

http://www.albahari.com/nutshell/ch20.aspx

In this code:

if(mutex.WaitOne(TimeSpan.Zero, true)) 
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
    mutex.ReleaseMutex();
} 
else 
{
    MessageBox.Show("only one instance at a time");
}

http://sanity-free.org/143/csharp_dotnet_single_instance_application.html

There is no inversion of the if/bool.

If waitone() is true, does that mean an instance is already running? If true is returned, the current thread will be blocked which would mean two processes calling the same app will both halt?

My understanding is as follows:

// Don't block the thread while executing code. 
//Let the code finish and then signal for another process to enter

What is the implication of no ! (returning true) and vice versa. Or in other words, what happens either way?

I know waitAll for example, waits for all threads to finish. Jon Skeet showed a good example of this on his site, which has stuck in my mind (credit to his explanations). So obviously waitOne waits for one thread to finish. The return value is what confuses me.

like image 516
GurdeepS Avatar asked May 02 '09 15:05

GurdeepS


2 Answers

To wait on a mutex means to wait until you can acquire it.

WaitOne on a Mutex will return true if the mutex could be acquired in the given time. If it couldn't, the method will return false. If the mutex was acquired, it's your responsibility to release the mutex when you're done with it.

If you can acquire a named mutex, nobody else owns it at the moment.

So, to recap. If you can acquire the mutex, the method returns true and you're the first/only instance of your application, in regards to your question.

If you cannot acquire the mutex, the method returns false and there is another application instance currently owning the mutex by that name.

like image 191
Lasse V. Karlsen Avatar answered Sep 28 '22 14:09

Lasse V. Karlsen


In the MSDN the description of mutex.WaitOne() is this:

Blocks the current thread until the current WaitHandle receives a signal.

And the return value is: true if the current instance receives a signal

So until the application is started once, it won't receive any signal from the mute.WaitOne() method. The thread is blocked like it says in the description.

So to answer you question:

What is the implication of no ! (returning true) and vice versa.

If you negate the mutex.WaitOne() method, then you will check this: If NOT waitOne(), it means you check if the waitOne doesn't respond with true.

I hope this is more clear now.

like image 23
Timotei Avatar answered Sep 28 '22 16:09

Timotei