Mutex (mutex)
A mutex is a mutually exclusive synchronization object that means that only one thread can get it at the same time.
Mutexes can be used when a shared resource can be accessed by only one thread at a time
Function:
Create a mutex that is in an acquired state
public Mutex ();
If owned is true, the initial state of the mutex is acquired by the main thread, otherwise it is not in the acquired state
Public Mutex (bool owned);
If you want to get a mutex lock. The WaitOne () method of the mutex lock should be called, which inherits from the Thread.waithandle class
It waits until the calling mutex can be fetched, so the method organizes the thread until the specified mutex is available, and if it does not need to have a mutex, it is freed with the ReleaseMutex method so that the mutex can be fetched by another thread.
Public Mutex (bool Owned,name,out flag);
Name is a mutex, that is, there is only one mutex mutex named name in the operating system, and if a thread gets the mutex of this name, the other thread cannot get the mutex, and must wait for that thread to release
Reference Code Experiment:
The code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceconsoleappmutex{classProgram {/// <summary> ///single-Instance execution testing/// </summary> /// <param name= "args" ></param>[STAThread]//indicates that the application's COM threading model is single-threaded apartment (STA) Static voidMain (string[] args) { stringName ="Test"; /** * Test 1 **/ BOOLFlag =false; //first parameter: true--gives the calling thread the initial ownership of the mutex//first parameter: The name of the mutex//Third parameter: Returns a value that returns true if the calling thread has been granted initial ownership of the mutexSystem.Threading.Mutex Mutex =NewSystem.Threading.Mutex (true, Name, outflag); if(flag) {Console.Write ("Running"); } Else{Console.Write ("another is Running"); System.Threading.Thread.Sleep ( the);//thread hangs for 5 secondsEnvironment.exit (1);//Exit Program} console.readline (); /** * Test 2 **/ //Create a single-instance object//System.Threading.Mutex mutex2 = new System.Threading.Mutex (false, name); //if (!mutex2. WaitOne (false))//do single-instance detection//{ //Console.Write ("Another is Running"); //System.Threading.Thread.Sleep (the);//thread hangs for 5 seconds//environment.exit (1);//Exit Program//} //Else//{ //Console.Write ("Running"); //} //console.readline (); } }}
Run the first instance of the application generated by the above code to get the results
Running
Keep the first running state, run the second instance, and get the result
Another is Running
The above code creates a mutex that, from the interpretation of its arguments, learns that the first calling thread will get the initial ownership of the mutex, and if it is not freed, the other threads are not entitled to the mutex.
1. Run two projects, and put the above code into the project, the first project to run, to obtain the results
Running
Keep the first running state, run the second project, get the results
Another is Running
2. System.Threading.Mutex Mutex in a project = new System.Threading.Mutex (True, "Test", out flag);
System.Threading.Mutex Mutex = new System.Threading.Mutex (True, "Test1", out flag);
And then the first project runs and gets the results.
Running
Keep the first running state, run the second project, get the results
Running
Indicates that the name of the mutex is unique in the system.
Mutex (mutex)