//method One: Only multiple processes are allowed to runusingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;namespaceduoyemianie{Static classProgram {/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {BOOLret; System.Threading.Mutex Mutex=NewSystem.Threading.Mutex (true, Application.productname, outret); if(ret) {System.Windows.Forms.Application.EnableVisualStyles (); //These two lines implement XP visual styleSystem.Windows.Forms.Application.DoEvents ();//These two lines implement XP visual styleSystem.Windows.Forms.Application.Run (NewLambrowser ()); //Main form for your program, if the console program does not use this sentencemutex. ReleaseMutex (); } Else{MessageBox.Show (NULL,"There is an application that is the same as this one that is already running, please do not run multiple programs at the same time. \ n \ nthe program is about to exit. ", Application.productname, MessageBoxButtons.OK, messageboxicon.warning); //the prompt message can be deleted. Application.exit ();//Exit Program } } }}
//method Two: Prevent multiple processes from running and activate the previous process when run repeatedlyusingSystem;usingSystem.Collections.Generic;usingSystem.Windows.Forms;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;usingSystem.Reflection;namespaceduoyemianie{Static classProgram {/// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//Get the running instance. Process instance =runninginstance (); if(Instance = =NULL) {System.Windows.Forms.Application.EnableVisualStyles (); //These two lines implement XP visual styleSystem.Windows.Forms.Application.DoEvents (); //there isn ' t another instance, show our form. System.Windows.Forms.Application.Run (NewLambrowser ()); } Else { //There is another instance of this process. Handlerunninginstance (instance); }} 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 processinchprocesses) { //Ignore The current process if(Process. Id! =Current . Id) {//Make sure, the process is running from the exe file. if(Assembly.getexecutingassembly (). Location.replace ("/","\\") ==Current . Mainmodule.filename) {//Return The other process instance. returnprocess; } } } //No Other instance is found, return null. return NULL;} Public Static voidHandlerunninginstance (Process instance) {//Make sure the window was not minimized or maximizedShowwindowasync (instance. Mainwindowhandle, Ws_shownormal); //Set the real intance to foreground windowSetForegroundWindow (instance. Mainwindowhandle);} [DllImport ("User32.dll")]Private Static extern BOOLShowwindowasync (IntPtr hWnd,intcmdshow); [DllImport ("User32.dll")]Private Static extern BOOLSetForegroundWindow (IntPtr hWnd);Private Const intWs_shownormal =1; }}
C # restricts programs to run only one instance (anti-multi-Open)