Private Static Void Globalmutex ()
{
// Whether to create mutex for the first time
Bool Newmutexcreated = False ;
String Mutexname = " Global \\ " + " Tenghoo " ;
Mutex = Null ;
Try
{
Mutex = New Mutex ( False , Mutexname, Out Newmutexcreated );
}
Catch (Exception ex)
{
Console. Write (ex. Message );
System. Threading. thread. Sleep ( 3000 );
Environment. Exit ( 1 );
}
// Create mutex for the first time
If (Newmutexcreated)
{
Console. writeline ( " ProgramStarted " );
// Todo: the task to be executed.
}
Else
{
Console. Write ( " The other window is already running and will be automatically closed 3 seconds later .. " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 1 " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 2 " );
System. Threading. thread. Sleep ( 1000 );
Console. Write ( " 3 " );
Environment. Exit ( 1 ); // Exit Program
}
}