Fork me on GitHub

C# 常用代码

学习备忘

1、判断程序是否运行

static void Main()
        {
            bool canCreateNew;
            System.Threading.Mutex myownmutex = new System.Threading.Mutex(true,Process.GetCurrentProcess().ProcessName , out canCreateNew);
            if (canCreateNew)
            {
                myownmutex.ReleaseMutex();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //由后台窗口启动
                Application.Run(new BackStage());
            }
            else
                MessageBox.Show("音量助手已在运行!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

Mutex

互斥体: 互斥体实现了“互相排斥”(mutual exclusion)同步的简单形式(所以名为互斥体(mutex))。互斥体禁止多个线程同时进入受保护的代码“临界区”(critical section)。(from 百度百科)

posted @ 2016-03-07 14:13  lumang  阅读(298)  评论(0编辑  收藏  举报