判断程序是否运行的代码 (有点单例模式的味道)

           

            string moduleName = Process.GetCurrentProcess().MainModule.ModuleName;
            string processName = System.IO.Path.GetFileNameWithoutExtension(moduleName);
            Process[] processes = Process.GetProcessesByName(processName);
            if (processes.Length > 1)
            {
                MessageBox.Show("程序已经运行!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }

      这段代码确实有种变相的单例模式的味道,不过好象一段代码,缺少了点AI的味道,应该把当前的那个实例调用出来就会更好了...

原文地址:https://www.cnblogs.com/mikejay1234/p/1493900.html