Mutex

Uses : for controlling an application only one instance.

For example

class Client{

private static Mutex _mutex;

static void Main(){

 if( ! IsStarted()){

 Current.ShutDown();

 return; 

}

.... 

}

 

private bool IsStarted(){

_mutex = new Mutex(true,"APPName",isCreatedNew);

return isCreatedNew; 

}

}

 

Mutex (): with a no-argument constructor obtained Mutex no name, but can not share data between processes in the form of variables, there is no name Mutex also called local (Local) Mutex. In addition, such create a Mutex, the creator of this example is not ownership, still you need to call WaitOne () to request ownership.


Mutex (Boolean initiallyOwned): above constructor, as it can only create a local Mutex without a name, can not be used for synchronization between processes. Boolean parameter is used to specify the creator created Mutex, whether immediate access to ownership, so the Mutex (false) is equivalent to Mutex ().


Mutex (Boolean initiallyOwned, String name) : In addition to this constructor can specify whether we get the initial ownership after it is created, but also that this Mutex a name. Only such named Mutex can be used by other applications in the domain of the program, so this is also called the global Mutex (Global) Mutex. If String is null or empty string, which is equivalent to create an unnamed Mutex. Because there may be other programs that precede you create a Mutex with the same name, so the Mutex instance returned may just point to the Mutex same name only. However, this constructor does not have any mechanism to tell us this. So, if you want to create a named Mutex, and Mutex we expect to know whether this is created by you, it is best to use either of the following two constructors one. Finally, note name is case-sensitive.


Mutex (Boolean initiallyOwned, String name, out Boolean createdNew): the same as the first two parameters above constructor, the third parameter is used to indicate whether out to obtain an initial ownership. The constructor should we use more in practice.


Mutex (Boolean initiallyOwned, String name, out Booldan createdNew, MutexSecurity): more out of this MutexSecurity parameters, but also due to the nature of the global Mutex determined. Because it can be accessed within the scope of the operating system, so it raises questions about the security of access, such as a program which is running Windows account can access the Mutex, whether you can modify this Mutext and so on.


Guess you like

Origin www.cnblogs.com/ljdong7/p/12618099.html