当前位置: 首页>>代码示例>>C#>>正文


C# Mutex.WaitOne方法代码示例

本文整理汇总了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();
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:25,代码来源:MutexTests.cs

示例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();
        }
    }
开发者ID:johnhhm,项目名称:corefx,代码行数:25,代码来源:MutexTests.cs

示例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);
        }
    }
开发者ID:johnhhm,项目名称:corefx,代码行数:15,代码来源:MutexTests.cs

示例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();
	}
开发者ID:AndroidHMD,项目名称:unity-android-aruco-sample,代码行数:27,代码来源:ArPlane.cs

示例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();
    }
开发者ID:geoffkizer,项目名称:coreclr,代码行数:32,代码来源:openmutexpos4.cs

示例6: Main

	static int Main (string [] args)
	{
		Mutex mutex = new Mutex ();
		mutex.WaitOne ();
		mutex.ReleaseMutex ();
		try {
			mutex.ReleaseMutex ();
			return 1;
		} catch (ApplicationException) {
			return 0;
		}
	}
开发者ID:mono,项目名称:gert,代码行数:12,代码来源:test.cs

示例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;
        }
    }
开发者ID:iamchenxin,项目名称:CShape6_Unity3D,代码行数:14,代码来源:Logger.cs

示例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;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:50,代码来源:waitone2.cs

示例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);
        }
    }
开发者ID:lgatto,项目名称:proteowizard,代码行数:18,代码来源:SingleInstanceHandler.cs

示例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;
		}
	}
开发者ID:cupsster,项目名称:Unity3D.IncrementalCompiler,代码行数:15,代码来源:Logger.cs

示例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");
         }
     }
 }
开发者ID:irium,项目名称:Pepita,代码行数:17,代码来源:TaskFileReplacer.cs

示例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");
         }
     }
 }
开发者ID:Z731,项目名称:NotifyPropertyWeaver,代码行数:17,代码来源:TaskFileReplacer.cs

示例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");
         }
     }
 }
开发者ID:paulcbetts,项目名称:Fody,代码行数:17,代码来源:TaskFileReplacer.cs

示例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;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:45,代码来源:waitone1.cs

示例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;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:44,代码来源:mutexctor2.cs


注:本文中的Mutex.WaitOne方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。