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
        }



  • 相关阅读:
    gnome-shell 使用 notify-send 发送桌面消息
    fedora 中从命令行中直接打开资源管理器
    wget curl 的 使用方法
    NFS 系统的搭建 on fedora; nfs 搭建 on ubuntu 1806;
    -bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory
    Linux 快速查看系统配置-熟悉新环境的配置
    2019-11-7-WPF-How-to-get-plain-text-from-RichTextBox
    2018-8-10-git-push-错误-hook-declined-
    2018-12-25-win2d-图片水印
    2018-8-10-win10-UWP-用Path画图
  • 原文地址:https://www.cnblogs.com/kevinzhwl/p/1756912.html
Copyright © 2011-2022 走看看