À l'aide de waitone() la méthode

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

Dans ce 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

Il n'y a pas d'inversion de la si/bool.

Si waitone() est vrai, cela signifie qu'une instance est déjà en cours d'exécution? Si true est retourné, le thread en cours sera bloqué, ce qui aboutirait à deux processus de l'appel de la même application permettra à la fois de stopper?

Ma compréhension est comme suit:

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

Quelle est l'implication de pas de ! (de retour) et vice versa. Ou en d'autres termes, qu'advient-il de toute façon?

Je sais waitAll par exemple, vous attend pour tous les threads à la fin. Jon Skeet a montré un bon exemple de cela sur son site, ce qui a coincé dans mon esprit (crédit à ses explications). Alors, évidemment, waitOne attend pour un fil à la fin. La valeur de retour est ce qui me confond.

OriginalL'auteur dotnetdev | 2009-05-02