zoukankan      html  css  js  c++  java
  • winform程序限制只能打开一个进程

    有很多方案,先来最傻瓜式的  :

    static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1)
                {  
                    MessageBox.Show("已经有一个运行了");
                }
                else
                {
                  
                    Application.Run(new  Form1());
                } 
    
            }
        }

    再来一种

    static class Program
        {
            [STAThread()]
            public static void Main()
            {
                bool ret;
                System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out  ret);
                if (ret)
                {
                    //System.Windows.Forms.Application.EnableVisualStyles();  //这两行实现  XP  可视风格 
                    //System.Windows.Forms.Application.DoEvents(); 
                    System.Windows.Forms.Application.Run(new Form1());
                    //  frmMain  为你程序的主窗体,如果是控制台程序不用这句 
                    m.ReleaseMutex();
                }
                else
                {
                    MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。
    
    这个程序即将退出。 ", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //  提示信息,可以删除。 
                    Application.Exit();//退出程序 
                }
            }
        }
  • 相关阅读:
    [GXYCTF2019]BabyUpload
    [GYCTF2020]Blacklist
    [极客大挑战 2019]HardSQL
    PHP实现MVC框架路由功能模式
    色环电阻的阻值识别
    python的内存回收机制
    配置Openfiler做ISCS实验
    windows server 2008r2 在vmware里自动关机
    VMware Workstation网络修改vlan id值
    linux的服务自动启动的配置
  • 原文地址:https://www.cnblogs.com/jara/p/3751733.html
Copyright © 2011-2022 走看看