The first way: The mutex mutex is implemented with only one instance of the process running
Static class Program {///<summary>//The main entry point of the application. </summary> [STAThread] static void Main () {Application.enablevisualstyles ( ); Application.setcompatibletextrenderingdefault (FALSE); Control the current program has been opened (that is, started)//mode 1: Using mutex mutex implementation at the same time only one process instance running//mutex (mutex)//mutex is a mutually exclusive synchronization object, which means that at the same time there is only There is one thread that can get it. Mutexes can be applied when a shared resource can only be accessed by one thread at a time bool flag = FALSE; System.Threading.Mutex Hmutex = new System.Threading.Mutex (True, Application.productname, out flag); BOOL B = Hmutex.waitone (0, false); /* Above parameter description: The first parameter "initiallyowned": true: Indicates whether the calling thread should have the initial ownership of the mutex (honestly not understood) the second parameter "name": Program unique Name, (in the current operating system The third parameter, "Creatednew", that determines the flag that runs repeatedly: Returns a value that returns (FALSE) if it detects that it has been started. */if (flag) {//does not start the same program Application.Run (new MainForm ()); } else {MessageBox.Show ("The current program is already running, do not run repeatedly.") "); Environment.exit (1);//Exit Program}}}
C # Winfo Determine whether the current program has started or exists in several ways