禁止启动多个winform项目进程程序.exe

1, 在winform项目中 Program.cs 文件里添加
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SMProjectSysetm
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //禁止启动多个项目进程
            Process[] process = Process.GetProcesses(); //获取所有进程
            int count = 0;
            foreach (Process item in process)
            {
                if (item.ProcessName == Process.GetCurrentProcess().ProcessName)
                {
                    count += 1;
                }
            }
            if (count > 1) //当前进程数量多于1个的时候,禁止再次打开一个进程
            {
                Application.Exit();
                return;
            }

            FrmLogin frmLogin = new FrmLogin();
            DialogResult result = frmLogin.ShowDialog();
            if (result == DialogResult.OK)
            {
                Application.Run(new FrmMain());
            }
        }
    }
}

 

posted @ 2024-06-18 16:05  龙卷风吹毁停车场  阅读(30)  评论(0)    收藏  举报