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.aspxMit WaitOne() -Methode
In diesem 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
Es gibt keine Umkehrung des if/Bool.
Wenn waitone() wahr ist, bedeutet das, dass bereits eine Instanz läuft? Wenn true zurückgegeben wird, wird der aktuelle Thread blockiert, was bedeuten würde, dass zwei Prozesse, die dieselbe App aufrufen, beide anhalten werden?
Mein Verständnis ist, wie folgt:
// Don't block the thread while executing code.
//Let the code finish and then signal for another process to enter
Was die Implikation nicht ist! (zurück wahr) und umgekehrt. Oder mit anderen Worten, was passiert in beiden Richtungen?
Ich weiß warten, zum Beispiel, wartet auf alle Threads zu beenden. Jon Skeet hat auf seiner Seite ein gutes Beispiel dafür gezeigt, was mir im Gedächtnis geblieben ist (Anerkennung für seine Erklärungen). Offensichtlich wartet waitOne darauf, dass ein Thread beendet wird. Der Rückgabewert ist was mich verwirrt.