本文整理汇总了C#中Mutex.ReleaseMutex方法的典型用法代码示例。如果您正苦于以下问题:C# Mutex.ReleaseMutex方法的具体用法?C# Mutex.ReleaseMutex怎么用?C# Mutex.ReleaseMutex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mutex
的用法示例。
在下文中一共展示了Mutex.ReleaseMutex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OpenExisting
public void OpenExisting()
{
string name = Guid.NewGuid().ToString("N");
Mutex resultHandle;
Assert.False(Mutex.TryOpenExisting(name, out resultHandle));
using (Mutex m1 = new Mutex(false, name))
{
using (Mutex m2 = Mutex.OpenExisting(name))
{
Assert.True(m1.WaitOne(FailedWaitTimeout));
Assert.False(Task.Factory.StartNew(() => m2.WaitOne(0), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Result);
m1.ReleaseMutex();
Assert.True(m2.WaitOne(FailedWaitTimeout));
Assert.False(Task.Factory.StartNew(() => m1.WaitOne(0), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Result);
m2.ReleaseMutex();
}
Assert.True(Mutex.TryOpenExisting(name, out resultHandle));
Assert.NotNull(resultHandle);
resultHandle.Dispose();
}
}
示例2: Main
static int Main (string [] args)
{
Mutex mutex = new Mutex ();
mutex.WaitOne ();
mutex.ReleaseMutex ();
try {
mutex.ReleaseMutex ();
return 1;
} catch (ApplicationException) {
return 0;
}
}
示例3: OpenExisting
public void OpenExisting()
{
const string Name = "MutexTestsOpenExisting";
Mutex resultHandle;
Assert.False(Mutex.TryOpenExisting(Name, out resultHandle));
using (Mutex m1 = new Mutex(false, Name))
{
using (Mutex m2 = Mutex.OpenExisting(Name))
{
Assert.True(m1.WaitOne());
Assert.False(Task.Factory.StartNew(() => m2.WaitOne(0), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Result);
m1.ReleaseMutex();
Assert.True(m2.WaitOne());
Assert.False(Task.Factory.StartNew(() => m1.WaitOne(0), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Result);
m2.ReleaseMutex();
}
Assert.True(Mutex.TryOpenExisting(Name, out resultHandle));
Assert.NotNull(resultHandle);
resultHandle.Dispose();
}
}
示例4: PosTest1
public bool PosTest1()
{
bool retVal = true;
Thread thread = null;
// Add your scenario description here
TestLibrary.TestFramework.BeginScenario("PosTest1: Construct a new Mutex instance with initiallyOwned set to true (ensure that the thread owns the mutex)");
using(m_Mutex = new Mutex(true))
{
try
{
do
{
if (null == m_Mutex)
{
TestLibrary.TestFramework.LogError("001", "Can not construct a new Mutex intance with initiallyOwned set to true.");
retVal = false;
break;
}
// Ensure initial owner of the mutex is current thread
// Create another thread to change value of m_SharedResource
thread = new Thread(new ThreadStart(ThreadProc));
thread.Start();
// Sleep 1 second to wait the thread get started
Thread.Sleep(c_DEFAULT_SLEEP_TIME);
if (m_SharedResource != c_DEFAULT_INT_VALUE)
{
TestLibrary.TestFramework.LogError("002", "Call Mutex(true) does not set current thread to be the owner of the mutex.");
retVal = false;
}
m_Mutex.ReleaseMutex();
} while (false); // do
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("003", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
finally
{
if (null != thread)
{
// Wait until all threads are terminated
thread.Join();
}
// Reset the value of m_SharedResource for further usage
m_SharedResource = c_DEFAULT_INT_VALUE;
}
}
return retVal;
}
示例5: Main
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new FormLineCanon());
// CurrentUser=new User();
// Giá trị luận lý cho biết ứng dụng này
// có quyền sở hữu Mutex hay không.
bool ownmutex;
// Tạo và lấy quyền sở hữu một Mutex có tên là Icon;
using (var mutex = new Mutex(true, "Icon", out ownmutex))
{
// Nếu ứng dụng sở hữu Mutex, nó có thể tiếp tục thực thi;
// nếu không, ứng dụng sẽ thoát.
if (ownmutex)
{
// Application.Run(new FormLogin());
Application.Run(new FormLogin());
//giai phong Mutex;
mutex.ReleaseMutex();
}
else
Application.Exit();
}
}
示例6: Main
public static int Main(String[] args)
{
Console.WriteLine("MutexSample.cs ...");
gM1 = new Mutex(true,"MyMutex");
gM2 = new Mutex(true);
Console.WriteLine(" - Main Owns gM1 and gM2");
AutoResetEvent[] evs = new AutoResetEvent[4];
evs[0] = Event1;
evs[1] = Event2;
evs[2] = Event3;
evs[3] = Event4;
MutexSample tm = new MutexSample( );
Thread t1 = new Thread(new ThreadStart(tm.t1Start));
Thread t2 = new Thread(new ThreadStart(tm.t2Start));
Thread t3 = new Thread(new ThreadStart(tm.t3Start));
Thread t4 = new Thread(new ThreadStart(tm.t4Start));
t1.Start( );
t2.Start( );
t3.Start( );
t4.Start( );
Thread.Sleep(2000);
Console.WriteLine(" - Main releases gM1");
gM1.ReleaseMutex( );
Thread.Sleep(1000);
Console.WriteLine(" - Main releases gM2");
gM2.ReleaseMutex( );
WaitHandle.WaitAll(evs);
Console.WriteLine("... MutexSample.cs");
return 0;
}
示例7: Main
static void Main()
{
bool isFirstInstance;
var mutex = new Mutex(true, "VolumeWheelEx", out isFirstInstance);
if(isFirstInstance) {
try {
Hook.SetHook();
var exit = false;
while(!exit) { try { exit = WinAPI.GetMessage(IntPtr.Zero, IntPtr.Zero, 0, 0) > 0; } catch(NullReferenceException) { /* Don't worry, GetMessage can sometimes returns NULL. */ } };
Hook.UnHook();
} catch(Exception e) {
//LOG file directly on desktop
System.IO.File.AppendAllText(Environment.GetEnvironmentVariable("userprofile") + "\\Desktop\\VolumeWheelEx.log", "[" + DateTime.Now.ToString() + "]\n" + e.Source + " -> " + e.ToString() + "\n" + e.StackTrace + "\n\n");
} finally {
mutex.ReleaseMutex();
}
} else {
try {
var thisProc = Process.GetCurrentProcess();
var prcs = Process.GetProcessesByName(thisProc.ProcessName);
for(int i = 0; i < prcs.Length; i++) {
if(prcs[i].Id != thisProc.Id) {
WinAPI.PostThreadMessage((uint)prcs[i].Threads[0].Id, WinAPI.WM_QUIT, UIntPtr.Zero, IntPtr.Zero);
}
}
} catch { }
}
}
示例8: ReuseMutexThread
public void ReuseMutexThread()
{
Console.WriteLine("Waiting to reuse mutex");
manualEvent.WaitOne();
bool exists;
Mutex mutex = new Mutex(true, mutexName, out exists);
if (exists)
{
Console.WriteLine("Error, created new mutex!");
success = 97;
}
else
{
mutex.WaitOne();
}
try
{
Console.WriteLine("Mutex reused {0}", exists);
mutex.ReleaseMutex();
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception: {0}", e);
success = 98;
}
exitEvent.Set();
}
示例9: Awake
void Awake()
{
SetupWebCamTexture();
InitializeAruco();
mutex_ = new Mutex();
thread_ = new Thread(() => {
try {
for (;;) {
Thread.Sleep(0);
if (!isArucoUpdated_) {
mutex_.WaitOne();
var num = aruco_detect(aruco_, false);
GetMarkers(num);
mutex_.ReleaseMutex();
isArucoUpdated_ = true;
}
}
} catch (Exception e) {
if (!(e is ThreadAbortException)) {
Debug.LogError("Unexpected Death: " + e.ToString());
}
}
});
thread_.Start();
}
示例10: Start
// Use this for initialization
void Start() {
m_mutex = new Mutex(true);
listPosition = new List<Vector3>();
m_mutex.ReleaseMutex();
constantZ = transform.position.z;
getPath();
m_nextPoint0 = transform.position;
responseFromIA = true;
initializeRays();
}
示例11: Ctor_ConstructWaitRelease
public void Ctor_ConstructWaitRelease()
{
using (Mutex m = new Mutex())
{
Assert.True(m.WaitOne(FailedWaitTimeout));
m.ReleaseMutex();
}
using (Mutex m = new Mutex(false))
{
Assert.True(m.WaitOne(FailedWaitTimeout));
m.ReleaseMutex();
}
using (Mutex m = new Mutex(true))
{
Assert.True(m.WaitOne(FailedWaitTimeout));
m.ReleaseMutex();
m.ReleaseMutex();
}
}
示例12: Ctor
public void Ctor()
{
using (Mutex m = new Mutex())
{
Assert.True(m.WaitOne());
m.ReleaseMutex();
}
using (Mutex m = new Mutex(false))
{
Assert.True(m.WaitOne());
m.ReleaseMutex();
}
using (Mutex m = new Mutex(true))
{
Assert.True(m.WaitOne());
m.ReleaseMutex();
m.ReleaseMutex();
}
Assert.Throws<ArgumentException>(() => new Mutex(false, new string('a', 1000)));
const string Name = "MutexTestsCtor";
bool createdNew;
using (Mutex m1 = new Mutex(false, Name, out createdNew))
{
Assert.True(createdNew);
using (Mutex m2 = new Mutex(false, Name, out createdNew))
{
Assert.False(createdNew);
}
}
using (Semaphore s = new Semaphore(1, 1, Name))
{
Assert.Throws<WaitHandleCannotBeOpenedException>(() => new Mutex(false, Name));
}
}
示例13: PosTest1
public bool PosTest1()
{
bool retVal = true;
Thread thread = null;
TestLibrary.TestFramework.BeginScenario("PosTest1: Wait Infinite");
try
{
do
{
m_Mutex = new Mutex();
thread = new Thread(new ThreadStart(SleepLongTime));
thread.Start();
if (m_Mutex.WaitOne(Timeout.Infinite) != true)
{
TestLibrary.TestFramework.LogError("001", "Can not wait Infinite");
retVal = false;
break;
}
m_Mutex.ReleaseMutex();
} while (false); // do
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
finally
{
// Wait for the thread to terminate
if (null != thread)
{
thread.Join();
}
if (null != m_Mutex)
{
m_Mutex.Dispose();
}
}
return retVal;
}
示例14: CreateMutexThread
public void CreateMutexThread()
{
Console.WriteLine("Inside thread which creates a mutex");
Mutex mutex = new Mutex(true, mutexName);
Console.WriteLine("Mutex created");
manualEvent.Set();
mutex.ReleaseMutex();
exitEvent.WaitOne();
}
示例15: Main
public static int Main(String[] args) {
Console.WriteLine("CO_Mutex.cs ...");
gMutex = new Mutex(false,"MyMutex");
try { gMutex.ReleaseMutex( ); }
catch (ApplicationException) { Console.WriteLine("o.k. AppEx"); }
CO_Mutex tm = new CO_Mutex();
Thread t1 = new Thread(new ThreadStart(tm.T1start));
Thread t2 = new Thread(new ThreadStart(tm.T2start));
t1.Start();
t2.Start();
AutoResetEvent[] evs = new AutoResetEvent[2];
evs[0] = Event1;
evs[1] = Event2;
WaitHandle.WaitAll(evs);
Console.WriteLine("... CO_Mutex.cs");
return 0;
}