日期:2014-05-18  浏览次数:21120 次

为什么ReleaseMutex不会释放锁?
我仅仅是想测试一下Mutex类的用法,代码如下:
C# code
static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                const string mutextName = "wwwwwwwwhhhwlllllll77777777ll9999llllwwww";
                Mutex m = new Mutex(true, mutextName);
                bool b = m.WaitOne(TimeSpan.Zero, true);
                MessageBox.Show(b.ToString());
                
                m.ReleaseMutex();
                
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }


第一次运行: b 的值为 True
不要关闭,再运行一次该程序。
发现 b 的值为 False.
为什么?



------解决方案--------------------
C# code
Mutex m = new Mutex(true, mutextName);