How to make sure that only one instance of application is running in C#
public static void Main(string[] args)
{
// ----------------------------------------------------
bool running;
Mutex mutex = new Mutex(true, “Your application name”, out running);
if (!running)
{
Console.WriteLine("Another instance is already running.");
Console.WriteLine (Messages.ExitMessage);
Console.ReadKey(false);
return;
}
// -----------------------------------------------------
Your all working code will go here
// -------- Don’t forget to include this
GC.KeepAlive(mutex); //1
mutex.ReleaseMutex(); //2
}
Note: Use only one either 1 or 2 both will works, but make sure one is present.
Now only one application will run at a time.
No comments:
Post a Comment