本文整理汇总了C#中Mutex.WaitOne方法的典型用法代码示例。如果您正苦于以下问题:C# Mutex.WaitOne方法的具体用法?C# Mutex.WaitOne怎么用?C# Mutex.WaitOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mutex
的用法示例。
在下文中一共展示了Mutex.WaitOne方法的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: 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();
}
}
示例3: AbandonExisting
public void AbandonExisting()
{
using (Mutex m = new Mutex())
{
Task.Factory.StartNew(() => m.WaitOne(), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Wait();
Assert.Throws<AbandonedMutexException>(() => m.WaitOne());
}
using (Mutex m = new Mutex())
{
Task.Factory.StartNew(() => m.WaitOne(), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Wait();
AbandonedMutexException ame = Assert.Throws<AbandonedMutexException>(() => WaitHandle.WaitAny(new[] { m }));
Assert.Equal(0, ame.MutexIndex);
}
}
示例4: 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();
}
示例5: 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();
}
示例6: Main
static int Main (string [] args)
{
Mutex mutex = new Mutex ();
mutex.WaitOne ();
mutex.ReleaseMutex ();
try {
mutex.ReleaseMutex ();
return 1;
} catch (ApplicationException) {
return 0;
}
}
示例7: Logger
public Logger()
{
mutex = new Mutex(true, "smcs");
if (mutex.WaitOne(0)) // check if no other process is owning the mutex
{
logginStyle = LogginStyle.Immediate;
DeleteLogFileIfTooOld();
}
else
{
logginStyle = LogginStyle.Retained;
}
}
示例8: 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;
}
示例9: Connect
/// <summary>
/// Launch a new instance using 'args' or consolidate 'args' into a recent instance.
/// </summary>
public void Connect(string[] args)
{
if (Launching == null)
return; // nothing to do
// create global named mutex
using (ipcMutex = new Mutex(false, ipcMutexGuid))
{
// if the global mutex is not locked, wait for args from additional instances
if (ipcMutex.WaitOne(0))
waitForAdditionalInstances(args);
else
sendArgsToExistingInstance(args);
}
}
示例10: Logger
public Logger()
{
mutex = new Mutex(true, "smcs");
if (mutex.WaitOne(0)) // check if no other process is owning the mutex
{
loggingMethod = LoggingMethod.Immediate;
DeleteLogFileIfTooOld();
}
else
{
pendingLines = new StringBuilder();
loggingMethod = LoggingMethod.Retained;
}
}
示例11: AddFile
public void AddFile(string directoryInfo)
{
using (var mutex = new Mutex(true, typeof (TaskFileReplacer).FullName))
{
if (!mutex.WaitOne(100))
{
return;
}
var allText = File.ReadAllLines(TaskFilePath);
var fileContainsDirectory = allText.Any(line => line == directoryInfo);
if (!fileContainsDirectory)
{
messageDisplayer.ShowInfo("PepitaGet: Restart of Visual Studio required to update PepitaGet.");
File.AppendAllText(TaskFilePath, directoryInfo + "\r\n");
}
}
}
示例12: AddFile
public void AddFile(DirectoryInfo directoryInfo)
{
using (var mutex = new Mutex(true, typeof (TaskFileReplacer).FullName))
{
if (!mutex.WaitOne(100))
{
return;
}
var allText = File.ReadAllLines(taskFilePath);
var fileContainsDirectory = allText.Any(line => string.Equals(line, directoryInfo.FullName, StringComparison.OrdinalIgnoreCase));
if (!fileContainsDirectory)
{
errorDisplayer.ShowInfo(string.Format("NotifyPropertyWeaver: Restart of Visual Studio required to update '{0}'.", Path.Combine(directoryInfo.FullName, "NotifyPropertyWeaverMsBuildTask.dll")));
File.AppendAllText(taskFilePath, directoryInfo.FullName + "\r\n");
}
}
}
示例13: AddFile
public void AddFile(string directoryInfo)
{
using (var mutex = new Mutex(true, typeof (TaskFileReplacer).FullName))
{
if (!mutex.WaitOne(100))
{
return;
}
var allText = File.ReadAllLines(TaskFilePath);
var fileContainsDirectory = allText.Any(line => string.Equals(line, directoryInfo, StringComparison.InvariantCultureIgnoreCase));
if (!fileContainsDirectory)
{
messageDisplayer.ShowInfo("Fody: Restart of Visual Studio required to update Fody.");
File.AppendAllText(TaskFilePath, directoryInfo + "\r\n");
}
}
}
示例14: PosTest1
public bool PosTest1()
{
bool retVal = true;
Thread thread = null;
TestLibrary.TestFramework.BeginScenario("PosTest1: WaitOne returns true when current instance receives a signal");
try
{
do
{
m_Mutex = new Mutex();
thread = new Thread(new ThreadStart(SignalMutex));
thread.Start();
if (m_Mutex.WaitOne() != true)
{
TestLibrary.TestFramework.LogError("001", "WaitOne returns false when current instance receives a signal.");
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();
}
m_Mutex.Dispose();
}
return retVal;
}
示例15: PosTest1
public bool PosTest1()
{
bool retVal = true;
// Add your scenario description here
TestLibrary.TestFramework.BeginScenario("PosTest1: Construct a new Mutex instance");
// create the mutex in unowned state
using (m_Mutex = new Mutex())
{
try
{
do
{
if (null == m_Mutex)
{
TestLibrary.TestFramework.LogError("001", "Can not construct a new Mutex intance");
retVal = false;
break;
}
// Ensure initial owner of the mutex is not the current thread
// This call should NOT block
m_Mutex.WaitOne();
m_Mutex.ReleaseMutex();
} while (false); // do
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("003", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
finally
{
// Reset the value of m_SharedResource for further usage
m_SharedResource = c_DEFAULT_INT_VALUE;
}
}
return retVal;
}