Method One: Prohibit multiple processes from running
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;namespaceOpen a new process {Static classProgram {/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {BOOLFlag; System.Threading.Mutex Mutex=NewSystem.Threading.Mutex (true, Application.productname, outflag); if(flag) {//enable visual styles for an applicationApplication.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); //Process all Windows messages currently in Message Queuingapplication.doevents (); Application.Run (NewForm1 ()); //release System.Threading.Mutex one timemutex. ReleaseMutex (); } Else{MessageBox.Show (NULL,"The same program is already running, please do not run multiple programs at the same time!\n\n this program is about to quit!", Application.productname, MessageBoxButtons.OK, messageboxicon.warning); Application.exit (); } } }}
Method Two: Prevent multiple processes from running and activate the previous process when run repeatedly
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;usingSystem.Diagnostics;usingSystem.Reflection;usingSystem.Runtime.InteropServices;namespaceOpen a new process {Static classProgram {/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {System.Diagnostics.Process instance=runninginstance (); if(Instance = =NULL) {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (NewForm1 ()); } Else{handlerunninginstance (instance); } } /// <summary> ///Gets the process instance that is currently running/// </summary> /// <returns></returns> Public StaticProcess runninginstance () {//gets the currently active processProcess current =process.getcurrentprocess (); //gets all processes for the specified process name on the current local computerProcess[] Processes =Process.getprocessesbyname (current. ProcessName); foreach(Process processinchprocesses) { //Ignore Current process if(Process. Id! =Current . Id) {if(Assembly.getexecutingassembly (). Location.replace ("/","\\") ==Current . Mainmodule.filename) {returnprocess; } } } //returns NULL if no other process with the same name exists return NULL; } //indicates that the attributed method is exposed as a static entry point by an unmanaged dynamic-link library (DLL)[DllImport ("User32.dll")] Private Static extern BOOLShowwindowasync (IntPtr hWnd,intcmdshow); [DllImport ("User32.dll")] Private Static extern BOOLSetForegroundWindow (IntPtr hWnd); Private Const intWs_shownormal =1; /// <summary> ///if another process with the same name is started, the previous instance is called/// </summary> /// <param name= "instance" ></param> Private Static voidHandlerunninginstance (Process instance) {//ensure that the form is not minimized or maximizedShowwindowasync (instance. Mainwindowhandle, Ws_shownormal); //Get A previously started process instance to the foreground windowSetForegroundWindow (instance. Mainwindowhandle); } }}
WinForm application prevents multiple processes from running