zoukankan      html  css  js  c++  java
  • c#启动单个程序(互斥机制)

        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {

                try
                {
                    bool ret;
                    System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out   ret);
                    if (ret)
                    {

                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new FrmMain());
                        m.ReleaseMutex();

                    }
                    else
                    {
                        MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        // 提示信息,可以删除。
                        System.Diagnostics.Process[] killprocess = System.Diagnostics.Process.GetProcessesByName(Application.ProductName);
                        foreach (System.Diagnostics.Process p in killprocess)
                        {
                            // if p.Id==Application.pro
                            int id = Process.GetCurrentProcess().Id;
                            if (id != p.Id)
                            {
                                p.Kill();
                                p.WaitForExit();
                            }

                        }

                        //Application.Exit();//退出程序  
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new FrmMain());
                        m.ReleaseMutex();

                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show("程序出现异常,即将关闭,请重新运行"+exp.ToString());
                    System.Environment.Exit(0);
                }

            }//main
        }



  • 相关阅读:
    【20171123】【GITC精华演讲】贝业新兄弟李济宏:如何做到企业信息化建设的加减乘除
    920记者招待会: 对话详解海尔张瑞敏首席的人单合一
    存货控制中的ABC分类释义
    对员工宽容的公司 都死掉了
    小型互联网公司的IT系统建设思路
    第三方物流是什么
    伟哥对RTO & RPO的通俗理解
    【20170506】贝业新兄弟IT总监李济宏:第三方家居物流的IT架构探索
    【Vegas原创】VirtualBox扩容、分割的整体方案
    数据通信基础(物理层)学习笔记
  • 原文地址:https://www.cnblogs.com/kevinzhwl/p/1756912.html
Copyright © 2011-2022 走看看