zoukankan      html  css  js  c++  java
  • winform程序防止重复运行

    用互斥法实现防止程序重复运行,使用内核对象Mutex可以防止同一个进程运行两次。注意:是名称相同的进程,而不是exe,因为exe程序可以改名。

    在Program.cs中修改

    首先添加using System.Threading;引用

    然后原内容改为下面所示

     static class Program
        {

            /// <summary>
            /// 互斥法防止程序重复运行
            private static Mutex myMutex;
            private static bool requestInitialOwnership = true;
            private static bool mutexWasCreated;
            /// </summary>
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {

    //原来代码
                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form1());

    //新代码

                myMutex = new Mutex(requestInitialOwnership, "myExe ", out   mutexWasCreated);
                if (mutexWasCreated)
                {
                    Application.Run (new Form1());
                    myMutex.WaitOne();
                }
            }
        }

  • 相关阅读:
    IP通信02
    h5网页 微信分享给好友,朋友圈-tp5
    微博常用链接
    Sublime Text3之安裝Emmet及使用技巧
    JS 写入到文件
    PHP之httpRequest
    图片上传预览
    滚动数字时钟
    旋转
    创建JavaScript标准对象--面试经常遇到的问题
  • 原文地址:https://www.cnblogs.com/enjoyprogram/p/2145463.html
Copyright © 2011-2022 走看看