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


C# Mutex.Dispose方法代码示例

本文整理汇总了C#中Mutex.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Mutex.Dispose方法的具体用法?C# Mutex.Dispose怎么用?C# Mutex.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Mutex的用法示例。


在下文中一共展示了Mutex.Dispose方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DisposeTest

    public static void DisposeTest()
    {
        var name = "MyCrazyMutexName" + Guid.NewGuid();
        var handle = new Mutex(true, name);

        handle.Dispose();

        Assert.False(Mutex.TryOpenExisting(name, out handle));
        // TODO: Better exceptions on .NET Native
        //Assert.Throws<ObjectDisposedException>(() => handle.WaitOne(0));
    }
开发者ID:johnhhm,项目名称:corefx,代码行数:11,代码来源:WaitHandle.cs

示例2: 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

示例3: 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

示例4: NegTest3

    public bool NegTest3()
    {
        bool retVal = true;
        int testInt = 0;

        TestLibrary.TestFramework.BeginScenario("NegTest3: Check ArgumentOutOfRangeException will be thrown if millisecondsTimeout is a negative number other than -1");

        try
        {
            testInt = TestLibrary.Generator.GetInt32();

            if (testInt > 0)
            {
                testInt = 0 - testInt;
            }

            if (testInt == -1)
            {
                testInt--;
            }

            m_Mutex = new Mutex();
            m_Mutex.WaitOne(testInt);

            TestLibrary.TestFramework.LogError("105", "ArgumentOutOfRangeException is not thrown if millisecondsTimeout is a negative number other than -1");
            TestLibrary.TestFramework.LogInformation("[LOCAL VARIABLES] testInt = " + testInt);
            retVal = false;
        }
        catch (ArgumentOutOfRangeException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("106", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation("[LOCAL VARIABLES] testInt = " + testInt);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }
        finally
        {
            if (null != m_Mutex)
            {
                m_Mutex.Dispose();
            }
        }

        return retVal;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:48,代码来源:waitone2.cs

示例5: NegTest1

    public bool NegTest1()
    {
        bool retVal = true;
        Thread thread = null;

        TestLibrary.TestFramework.BeginScenario("NegTest1: AbandonedMutexException should be thrown if a thread exited without releasing a mutex");

        try
        {
            m_Mutex = new Mutex();

            thread = new Thread(new ThreadStart(NeverReleaseMutex));
            thread.Start();
            Thread.Sleep(c_DEFAULT_SLEEP_TIME / 5); // To avoid race
            m_Mutex.WaitOne(Timeout.Infinite);

            // AbandonedMutexException is not thrown on Windows 98 or Windows ME
            //if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            //{
                TestLibrary.TestFramework.LogError("101", "AbandonedMutexException is not thrown if a thread exited without releasing a mutex");
                retVal = false;
            //}
        }
        catch (AbandonedMutexException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }
        finally
        {
            if (null != thread)
            {
                thread.Join();
            }

            if (null != m_Mutex)
            {
                m_Mutex.Dispose();
            }
        }

        return retVal;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:47,代码来源:waitone2.cs

示例6: PosTest7

    public bool PosTest7()
    {
        bool retVal = true;
        Thread thread = null;

        TestLibrary.TestFramework.BeginScenario("PosTest7: Wait infinite time when another thread is in nondefault managed context");

        try
        {
            m_Mutex = new Mutex();

            thread = new Thread(new ThreadStart(CallContextBoundObjectMethod));
            thread.Start();
            Thread.Sleep(c_DEFAULT_SLEEP_TIME / 5); // To avoid race
            if (true != m_Mutex.WaitOne(Timeout.Infinite))
            {
                m_Mutex.ReleaseMutex();
                TestLibrary.TestFramework.LogError("012", "WaitOne returns false when wait infinite time when another thread is in nondefault managed context");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("013", "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,代码行数:43,代码来源:waitone2.cs

示例7: PosTest3

    public bool PosTest3()
    {
        bool retVal = true;
        Thread thread = null;

        TestLibrary.TestFramework.BeginScenario("PosTest3: Wait some finite time will quit for timeout");

        try
        {
            m_Mutex = new Mutex();

            thread = new Thread(new ThreadStart(SignalMutex));
            thread.Start();
            Thread.Sleep(c_DEFAULT_SLEEP_TIME / 5); // To avoid race
            if (false != m_Mutex.WaitOne(c_DEFAULT_SLEEP_TIME / 10))
            {
                m_Mutex.ReleaseMutex();
                TestLibrary.TestFramework.LogError("004", "WaitOne returns true when wait time out");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("005", "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,代码行数:43,代码来源:waitone2.cs

示例8: PosTest2

    public bool PosTest2()
    {
        bool retVal = true;
        Thread thread = null;

        TestLibrary.TestFramework.BeginScenario("PosTest2: Wait some finite time");

        try
        {
            m_Mutex = new Mutex();

            thread = new Thread(new ThreadStart(SignalMutex));
            thread.Start();

            m_Mutex.WaitOne(2 * c_DEFAULT_SLEEP_TIME);

            m_Mutex.ReleaseMutex();
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("003", "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,代码行数:40,代码来源:waitone2.cs

示例9: SetLiveTile

    /// <summary>
    /// Set the live tile status
    /// </summary>
    /// <param name="fromSettings">indicate if called from seettings window, which means the user will get notifications</param>
    public static void SetLiveTile(bool fromSettings)
    {
        string isTileEnabled;
        bool tileSettingsExist = IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("EnableTile", out isTileEnabled);
        string taskName = "Waze Periodic Task";

        Mutex mLiveTileStorageMutex = new Mutex(true,"LiveTileStorageMutex");

        try
        {

            mLiveTileStorageMutex.WaitOne();

            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

            if (!isf.DirectoryExists("LiveTile"))
            {
                isf.CreateDirectory("LiveTile");
            }

            // Save the home name in selected language so the task can search for it later on.
            using (IsolatedStorageFileStream fsHomeName =  isf.OpenFile("LiveTile\\HomeName", FileMode.Create,FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fsHomeName))
                {
                    sw.Write(LanguageResources.Instance.Translate("Home"));
                }

            }

            // Save the home name in selected language so the task can search for it later on.
            using (IsolatedStorageFileStream fsWorkName = isf.OpenFile("LiveTile\\WorkName", FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fsWorkName))
                {
                    sw.Write(LanguageResources.Instance.Translate("Work"));
                    sw.Flush();
                }

            }

            // Save the refresh interval so the task can search for it later on.
            using (IsolatedStorageFileStream fsInterval =  isf.OpenFile("LiveTile\\Interval",FileMode.Create,FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fsInterval))
                {
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("TileRefreshInterval"))
                    {
                        sw.Write(IsolatedStorageSettings.ApplicationSettings["TileRefreshInterval"].ToString());
                    }
                    else
                    {
                        sw.Write("10");
                    }
                    sw.Flush();
                }

            }
        }
        finally
        {
            mLiveTileStorageMutex.ReleaseMutex();
            mLiveTileStorageMutex.Dispose();
        }

        PeriodicTask periodicTask = new PeriodicTask(taskName);
        periodicTask.Description = "Waze Task to update Live Tile";

        // Make the task alive for atnoher 14 days. If the user didn't run waze for 14 days, the task will auto delete by the system.
        periodicTask.ExpirationTime = System.DateTime.Now.AddDays(14);

        // If the agent is already registered with the system, remove it first

        if (ScheduledActionService.Find(periodicTask.Name) != null)
        {
            ScheduledActionService.Remove(taskName);
        }

        // Clear the current tile

        ShellTile currentTiles = ShellTile.ActiveTiles.First();
        StandardTileData tilesUpdatedData = new StandardTileData
        {
            Title = "Waze",
            BackgroundImage = new Uri("waze_logo.png", UriKind.Relative),
            Count = 0,
            BackTitle = string.Empty,
            BackContent = string.Empty

        };

        currentTiles.Update(tilesUpdatedData);

        // Place the call to Add in a try block in case the user has disabled agents, but selected to turn it on from the app:
        try
        {
//.........这里部分代码省略.........
开发者ID:ckeboss,项目名称:WazeWP7,代码行数:101,代码来源:Syscalls.cs

示例10: Main


//.........这里部分代码省略.........

            string executableName = Application.ExecutablePath;
            var executableFileInfo = new FileInfo(executableName);
            ExecutableDirectory = executableFileInfo.DirectoryName;

            bool ei = (!Directory.Exists(AppDataPath) || !Directory.Exists(AppDataPath + @"XML\") ||
                       !File.Exists(AppDataPath + @"XML\config.xml"));
            if (ei)
                EnsureInstall(true);

            bool silentstartup = false;

            string command = "";
            if (args.Length > 0)
            {
                //if (args[0].ToLower().Trim() == "-firstrun" && !ei)
                //    EnsureInstall(false);
                if (args[0].ToLower().Trim() == "-reset" && !ei)
                {
                    if (firstInstance)
                    {
                        if (
                            MessageBox.Show("Reset iSpy? This will overwrite all your settings.", "Confirm",
                                            MessageBoxButtons.OKCancel) == DialogResult.OK)
                            EnsureInstall(true);
                    }
                    else
                    {
                        MessageBox.Show("Please exit iSpy before resetting it.");
                    }
                }
                if (args[0].ToLower().Trim() == "-silent" || args[0].ToLower().Trim('\\') == "s")
                {
                    if (firstInstance)
                    {
                        silentstartup = true;
                    }
                }
                else
                {
                    for (int index = 0; index < args.Length; index++)
                    {
                        string s = args[index];
                        command += s + " ";
                    }
                }
            }

            if (!firstInstance)
            {
                if (!String.IsNullOrEmpty(command))
                {
                    File.WriteAllText(AppDataPath + "external_command.txt", command);
                    //ensures pickup by filesystemwatcher
                    Thread.Sleep(1000);
                }

                Application.Exit();
                return;
            }

            if (VlcHelper.VlcInstalled)
                VlcHelper.AddVlcToPath();

            File.WriteAllText(AppDataPath + "external_command.txt", "");

            // in case our https certificate ever expires or there is some other issue
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            WriterMutex = new Mutex();
            Application.ThreadException += ApplicationThreadException;

            var mf = new MainForm(silentstartup, command);
            Application.Run(mf);
            WriterMutex.Close();
            WriterMutex.Dispose();

        }
        catch (Exception ex)
        {
            try
            {
                Log.Error("",ex);//MainForm.LogExceptionToFile(ex);
            } catch
            {

            }
            while (ex.InnerException != null)
            {
                try
                {
                    Log.Error("",ex);//MainForm.LogExceptionToFile(ex);
                }
                catch
                {

                }
            }
        }
    }
开发者ID:vmail,项目名称:main,代码行数:101,代码来源:Program.cs

示例11: Main

    public static void Main()
    {
        Mutex mutex = null;
        var shouldWait = true;
        try {
          bool createdNew;
          mutex = new Mutex(true, MUTEX_NAME, out createdNew);

          if (createdNew == false) {
        OutputMessage("updatechromium is already running.");
        return;
          }

          shouldWait = Exec();
        }
        catch (Exception e) {
          OutputError(e);
        }
        finally {
          if (shouldWait) {
        while (Console.KeyAvailable) Console.ReadKey();
        OutputMessage("");
        OutputMessage("Press enter to exit");
        Console.ReadLine();
          }

          if (mutex != null) {
        try {
          mutex.ReleaseMutex();
        }
        catch (Exception e1) {
          OutputError(e1);
        }
        try {
          mutex.Dispose();
        }
        catch (Exception e1) {
          OutputError(e1);
        }
          }
        }
    }
开发者ID:hazychill,项目名称:updatechromium,代码行数:42,代码来源:updatechromium.cs


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