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();
                }
            }
        }

  • 相关阅读:
    MOSS 2013研究系列动态修改WebConfig(上) 欧阳锋
    MOSS 2013研究系列MOSS 2013安装篇 欧阳锋
    GPIO
    [转]vi/vim使用进阶: 在VIM中使用GDB调试 – 使用pyclewn
    建立openwrt虚拟环境
    ebtables基本使用
    LFS小记
    Autoconf & Automake使用小记
    Packet Filter小记
    Web技术整理
  • 原文地址:https://www.cnblogs.com/enjoyprogram/p/2145463.html
Copyright © 2011-2022 走看看