Method 1. Code Change Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Windows. forms;
Using System. diagnostics;
Using System. reflection;
Namespace Windowsformsapplicationtest
{
Static Class Program
{
/// <Summary>
/// The main entry point for the application.
/// </Summary>
[Stathread]
Static Void Main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault ( False );
// Prevents multiple applications from being started and sorted
If (Runninginstance () ! = Null )
{
System. Windows. Forms. MessageBox. Show ( " There is alreadyProgramRunning " );
Return ;
}
Application. Run (NewForm1 ());
}
Public StaticProcess runninginstance ()
{
Process current=Process. getcurrentprocess ();
Process [] Processes=Process. getprocessesbyname (current. processname );
// Loop through the running processes in with the same name
Foreach (Process In Processes)
{
// Ignore the current process
If (Process. ID ! = Current. ID)
{
// Make sure that the process is running from the EXE file.
If (Assembly. getexecutingassembly (). Location. Replace ( " / " , " \\ " ) = Current. mainmodule. filename)
{
// Return the other process instance.
Return Process;
}
}
}
//No other instance was found, return null.
Return Null;
}
}
}
Method 2 UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. Windows. forms;
UsingSystem. Threading;
Namespace Testform. treeviewandmdiform
{
Static Class Program
{
/// <Summary>
/// The main entry point of the application.
/// </Summary>
[Stathread]
Static Void Main ()
{
Bool Creatednew;
Mutex m = New Mutex ( True , " Test " , Out Creatednew );
If (Creatednew)
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault ( False );
Application. Run ( New Form1 ());
M. releasemutex ();
}
Else
{
MessageBox. Show ("Only one instance can be run!","System prompt");
}
}
}
}