C# 最简单的使程序单进程运行的方法

 

1.代码1

static void Main()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (process.MainModule.FileName == current.MainModule.FileName)
                    {
                        MessageBox.Show("程序已经运行", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

2.代码2

            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (process.MainModule.FileName == current.MainModule.FileName)
                    {
                        LogOperate.Error("===》当前进程已经启动");
                        return;
                    }
                }
            }

 

更多:

C# 异步编程Task整理(二)异常捕捉

C# 异步编程Task整理(一)

C# Process获取当前进程信息

posted @ 2019-05-18 15:16  天马3798  阅读(880)  评论(0编辑  收藏  举报