Method 1: prohibit multiple processes from running
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Windows. forms; Namespace Start a new process { Static Class Program { /// <Summary> /// ApplicationProgram. /// </Summary> [Stathread] Static Void Main (){ Bool Flag; system. Threading. mutex = New System. Threading. mutex ( True , Application. productname, Out Flag ); If (FLAG ){ // Enable the visual style of the application Application. enablevisualstyles (); application. setcompatibletextrenderingdefault ( False );// Process all Windows messages in the current Message Queue Application. doevents (); application. Run ( New Form1 ()); // Release system. Threading. mutex once Mutex. releasemutex ();} Else {MessageBox. Show ( Null ," The same program is already running. Please do not run multiple programs at the same time! \ N this program is about to exit! ", Application. productname, messageboxbuttons. OK, messageboxicon. Warning); application. Exit ();}}}}
Method 2: prohibit multiple processes from running and activate the previous processes when they are repeated.
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Windows. forms; Using System. diagnostics; Using System. reflection; Using System. runtime. interopservices; Namespace Start a new process { Static Class Program {/// <Summary> /// Main entry point of the application. /// </Summary> [Stathread] Static Void Main () {system. Diagnostics. Process instance = runninginstance (); If (Instance = Null ) {Application. enablevisualstyles (); application. setcompatibletextrenderingdefault ( False ); Application. Run ( New Form1 ());} Else {Handlerunninginstance (Instance );}} /// <Summary> /// Obtain the currently running process instance /// </Summary> /// <Returns> </returns> Public Static Process runninginstance (){ // Obtain the active process Process current = process. getcurrentprocess (); // Obtain all processes with the specified process name on the current local computer Process [] processes = process. getprocessesbyname (current. processname ); Foreach (ProcessIn Processes ){ // Ignore the current process If (Process. ID! = Current. ID ){ If (Assembly. getexecutingassembly (). Location. Replace (" / "," \\ ") = Current. mainmodule. filename ){ Return Process ;}}} // If no other process with the same name exists, null is returned. Return Null ;}// Indicates that this attribute method is made public by an unmanaged dynamic link library (DLL) as a static entry point. [Dllimport (" User32.dll ")] Private Static Extern Bool Showwindowasync (intptr hwnd, Int Cmdshow); [dllimport (" User32.dll ")] Private Static Extern Bool Setforegroundwindow (intptr hwnd ); Private Const Int Ws_shownormal = 1; /// <Summary> /// If another process with the same name starts, the previous instance is called. /// </Summary> /// <Param name = "instance"> </param> Private Static Void Handlerunninginstance (process instance ){ // Ensure that the form is not minimized or maximized Showwindowasync (instance. main1_whandle, ws_shownormal ); // Get the previously started process instance to the front-end window Setforegroundwindow (instance. main1_whandle );}}}