C # mutual exclusion operation

xiaoxiao2021-03-06  52

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using system.threading;

Namespace Exam_ User Programs can only be run

{

Pulic Class Forms: System.Windows.Forms.form

{

[Stathread]

Static void

Main

()

{

Bool creatednew;

Mutex M = New Mutext (True, "Test", Out CreatedNew);

IF (creatednew)

{

Application.run (New Form1 ());

m.releasemutex ();

}

Else

{

Messagebox.show ("This program only allows only one to run");

}

}

}

}

The program creates a mutective variable M by Mutex M = New Mutext (True, "Test", Out CreatedNew; statement, where the True parameter indicates the initial dependent right of the calling thread has a mutex, TEST is a mutex, and The Boolean value of whether the calling thread has been authorized to authorize the initial dependency of the mutex in the createDNew variable. Then determine whether the variable value is determined whether or not to start the program, if true, the present instance is not running, start the program via Application.Run (New Form1 ()) statement; otherwise, a dialog is displayed and the program is running.

转载请注明原文地址:https://www.9cbs.com/read-116305.html

New Post(0)