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.aspxUtilizando el método
WaitOne() En este código:
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
No hay inversión de la si/bool.
Si waitone() es verdadero, ¿eso significa que ya se está ejecutando una instancia? Si se devuelve verdadero, el hilo actual se bloqueará, lo que significa que dos procesos que invocan la misma aplicación se detendrán.
Mi opinión es la siguiente:
// Don't block the thread while executing code.
//Let the code finish and then signal for another process to enter
Cuál es la implicación de que no! (regresando verdadero) y viceversa. O en otras palabras, ¿qué sucede de cualquier manera?
Sé esperarTodo por ejemplo, espera a que finalicen todos los hilos. Jon Skeet mostró un buen ejemplo de esto en su sitio, que se ha quedado grabado en mi mente (crédito a sus explicaciones). Así que, obviamente, waitOne espera a que termine un hilo. El valor de retorno es lo que me confunde.