现在的位置: 首页 > 综合 > 正文

只允许运行一个实例的程序(互斥程序)

2018年04月05日 ⁄ 综合 ⁄ 共 8695字 ⁄ 字号 评论关闭
文章目录

有的时候我们需要编写出的应用程序在同一时间只允许运行一个实例,运行第二个实例时提示用户“该程序已经运行”。这是很老土的表示方法,但很灵验。
我找到了三种限制程序实例被多次运行的方法,它们有各自的优点,一会儿我会用一个控制台程序做例子,分别讨论这三种方法。有一种比较流行的方式,解决另人
恶心的“该程序已经运行”的对话框,比如千千静听,如果程序被第二次运行,它会直接将已经打开的程序前置,这样做确实温柔许多。我采用第二种方法来实现了
这个效果,就是第二个那个WinForm的例子。

示例1


示例1的效果图

  1. /*
  2. * 标题:只允许运行一个实例的程序(互斥程序)示例
  3. * 原文:http://hi.baidu.com/wingingbob/blog/item/943f5f2396637846925807cf.html
  4. */
  5. #define METHOD1    //方法一
  6. //#define METHOD2    //方法二
  7. //#define METHOD3    //方法三
  8. using
    System;   
  9. using
    System.Text;   
  10. namespace
    ConsoleApplication1   
  11. {   
  12.     
    class
    Program   
  13.      {   
  14.         
    static

    void
    Main(
    string
    [] args)   
  15.          {   
  16.              Console.WriteLine(
    "==============="
    );   
  17.              Console.WriteLine(
    "互斥程序示例"
    );   
  18.              Console.WriteLine(
    "==============="
    );
  19. #if METHOD1      //方法一:同步基元
  20.             
    bool
    runone;   
  21.              System.Threading.Mutex run =
    new
    System.Threading.Mutex(
    true
    ,
    "只允许一个实例的程序_Bob"
    ,
    out
    runone);   
  22.             
    if
    (runone)   
  23.              {   
  24.                  run.ReleaseMutex();   
  25.                  MyMain();           
    //正常运行
  26.              }   
  27.             
    else
  28.              {   
  29.                  PrintWarnning();    
    //输出警告
  30.              }
  31. #elif METHOD2    //方法二:判断进程
  32.              System.Diagnostics.Process theProcess =
    null
    ;   
  33.              System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();    
    //当前进程
  34.              System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();     
    //所有进程
  35.              var myFilename = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace(
    "/"
    , @
    "/"); //得到当前进程名"
  36.             
    foreach
    (var process
    in
    processes)   
  37.              {   
  38.                 
    //查找与当前进程相同名称的进程
  39.                 
    if
    (process.Id != current.Id && process.ProcessName == current.ProcessName)   
  40.                  {   
  41.                     
    //确认相同名称进程的程序运行位置是否一样
  42.                     
    try
  43.                      {   
  44.                         
    if
    (myFilename == process.MainModule.FileName)   
  45.                          {   
  46.                              theProcess = process;    
    //找到了此程序的另一个进程
  47.                          }   
  48.                      }   
  49.                     
    catch
    { }   
  50.                  }   
  51.              }   
  52.             
    if
    (theProcess ==
    null
    )   
  53.              {   
  54.                  MyMain();           
    //正常运行
  55.              }   
  56.             
    else
  57.              {   
  58.                  PrintWarnning();    
    //输出警告
  59.              }
  60. #elif METHOD3    //方法三:全局原子法
  61.             
    if
    (NativeMethods.GlobalFindAtomW(
    "只允许一个实例的程序_Bob"
    ) == 0)
    //没找到原子"只允许一个实例的程序"
  62.              {   
  63.                  var Atom = NativeMethods.GlobalAddAtomW(
    "只允许一个实例的程序_Bob"
    );  
    //添加原子"只允许一个实例的程序"
  64.                  MyMain();           
    //正常运行
  65.                  NativeMethods.GlobalDeleteAtom(Atom);   
    //一定记得程序退出时删除你的全局原子,否则只有在关机时它才会被清除。
  66.              }   
  67.             
    else
  68.              {   
  69.                 
    //NativeMethods.GlobalDeleteAtom(NativeMethods.GlobalFindAtomW("只允许一个实例的程序_Bob"));
  70.                  PrintWarnning();    
    //输出警告
  71.              }
  72. #endif
  73.          }   
  74.         
    /// <summary>
  75.         
    /// 输出警告
  76.         
    /// </summary>
  77.         
    private

    static

    void
    PrintWarnning()   
  78.          {   
  79.              Console.ForegroundColor = ConsoleColor.Red;   
  80.              Console.WriteLine(
    "警告:已经运行了一个实例了。"
    );   
  81.              Console.ReadLine();   
  82.          }   
  83.         
    /// <summary>
  84.         
    /// 正常运行
  85.         
    /// </summary>
  86.         
    private

    static

    void
    MyMain()   
  87.          {   
  88.              Console.WriteLine(
    "正常运行..."
    );   
  89.              Console.ReadLine();   
  90.          }
  91. #if METHOD3      //方法三:全局原子法
  92.          #region 查找原子
  93.         
    public
    partial
    class
    NativeMethods   
  94.          {   
  95.             
    /// Return Type: ATOM->WORD->unsigned short
  96.             
    ///lpString: LPCWSTR->WCHAR*
  97.              [System.Runtime.InteropServices.DllImportAttribute(
    "kernel32.dll"
    , EntryPoint =
    "GlobalFindAtomW"
    )]   
  98.             
    public

    static

    extern

    ushort

    GlobalFindAtomW([System.Runtime.InteropServices.InAttribute()]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]

    string
    lpString);   
  99.             
    /// Return Type: ATOM->WORD->unsigned short
  100.             
    ///lpString: LPCSTR->CHAR*
  101.              [System.Runtime.InteropServices.DllImportAttribute(
    "kernel32.dll"
    , EntryPoint =
    "GlobalFindAtomA"
    )]   
  102.             
    public

    static

    extern

    ushort

    GlobalFindAtomA([System.Runtime.InteropServices.InAttribute()]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]

    string
    lpString);   
  103.          }
  104.          #endregion
  105.          #region 添加原子
  106.         
    public
    partial
    class
    NativeMethods   
  107.          {   
  108.             
    /// Return Type: ATOM->WORD->unsigned short
  109.             
    ///lpString: LPCWSTR->WCHAR*
  110.              [System.Runtime.InteropServices.DllImportAttribute(
    "kernel32.dll"
    , EntryPoint =
    "GlobalAddAtomW"
    )]   
  111.             
    public

    static

    extern

    ushort

    GlobalAddAtomW([System.Runtime.InteropServices.InAttribute()]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]

    string
    lpString);   
  112.             
    /// Return Type: ATOM->WORD->unsigned short
  113.             
    ///lpString: LPCSTR->CHAR*
  114.              [System.Runtime.InteropServices.DllImportAttribute(
    "kernel32.dll"
    , EntryPoint =
    "GlobalAddAtomA"
    )]   
  115.             
    public

    static

    extern

    ushort

    GlobalAddAtomA([System.Runtime.InteropServices.InAttribute()]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]

    string
    lpString);   
  116.          }
  117.          #endregion
  118.          #region 删除原子
  119.         
    public
    partial
    class
    NativeMethods   
  120.          {   
  121.             
    /// Return Type: ATOM->WORD->unsigned short
  122.             
    ///nAtom: ATOM->WORD->unsigned short
  123.              [System.Runtime.InteropServices.DllImportAttribute(
    "kernel32.dll"
    , EntryPoint =
    "GlobalDeleteAtom"
    )]   
  124.             
    public

    static

    extern

    ushort
    GlobalDeleteAtom(
    ushort
    nAtom);   
  125.          }
  126.          #endregion
  127. #endif
  128.      }   
  129. }  

调试说明

看到6、7、8三行的预编译指令了吗?你可以通过注释、取消注释这三行来测试三种不同的方法。这段代码是完整的控制台程序源码,你可以使用csc命令来直接编译。每次将编译出来的程序同时打开数个,看输入结果。

方法一:同步基元

我认为最简单有效的方法。同步基元Mutex在MSDN中的解释是:“当两个或更多线程需要同时访问一个共
享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源。Mutex
是同步基元,它只向一个线程授予对共享资源的独占访问权。如果一个线程获取了互斥体,则要获取该互斥体的第二个线程将被挂起,直到第一个线程释放该互斥
体。”源程序第26行,当指定的命名系统互斥体已存在时,第三个参数会返回false,代码就会执行到第34条,输出“已经运行了一个实例了”的提示。具
体的方法请参见Mutex的构造函数

方法二:判断进程

进程判断是一个不错的方式,因为当你得到了这个进程,就可以对此进程进行一些操作,比如示例2的那种将已运行程序窗口置前的效果。但它最大的不足是,虽然
可以通过进程名(不同程序的进程名也可以相同的)和主程序集中存储的执行文件名信息来做判断,但是如果我的程序是多线程的(比如Chrome浏览器),便
无法正确处理下去。这种情况更适合使用方法一。在测试此方法的时候,需要说明一点的,第43行,得到当前进程名。如果你是在VS环境下调试,那么第一次得
到的进程文件名会是VS宿主的程序文件名,也就是“只允许一个实例的程
序.vshost.exe”。而它并不是我们期待得到的程序文件名“只允许一个实例的程序.exe”。此时再启动新实例,该行代码才会获得“只允
许一个实例的程序.exe”文件名。所以你需要启动第三个调试程序才会在52行判断成功。这并不是代码缺陷,特此说明。您可以直接运行bin目录中生成的
程序进行测试,没有问题的。

方法三:全局原子法

算是非常古老的方法,就是利用Windows给我们提供的全局原子来进行判断。我们在程序启动时,先判断
自己定义的一个全局原子(少于255字符)是否已经存在Windows系统中,如果不存在,当然程序就是第一次运行,注册自己定义的全局原子,正常加载程
序;否则就认为程序已经运行了,输出“已经运行了一个实例了”的提示。重要的是,主程序在退出时一定记得将我们定义的全局原子删除,否则只有在
Windows系统关机时,该全局原子才会被释放。这样一来,如果我们编写的程序被意外中止(比如程序本身的缺陷导致,软件冲突,或者被强制结束进程),
全局原子没被正确释放,那就只好等着机器重启了。因此我不推荐此方法。
此方法使用了三个Windows API函数:查找原子(GlobalFindAtom)、添加原子(GlobalAddAtom)和删除原子(GlobalDeleteAtom),你可以使用P/Invoke工具自己查找,然后帖到代码里。

 

示例2


示例2的效果图

  1. using
    System;   
  2. using
    System.Windows.Forms;   
  3. namespace
    WindowsFormsApplication1   
  4. {   
  5.     
    static

    class
    Program   
  6.      {   
  7.         
    /// <summary>
  8.         
    /// 应用程序的主入口点。
  9.         
    /// </summary>
  10.          [STAThread]   
  11.         
    static

    void
    Main()   
  12.          {   
  13.              var theProcess = GetRunningProcess();   
    //得到已经运行的进程
  14.             
    if
    (theProcess !=
    null
    )   
  15.              {   
  16.                 
    //应该程序已经运行,将其主窗口置前
  17.                  NativeMethods.ShowWindowAsync(theProcess.MainWindowHandle, 1);   
  18.                  NativeMethods.SetForegroundWindow(theProcess.MainWindowHandle);   
  19.              }   
  20.             
    else
  21.              {   
  22.                 
    //正常运行程序
  23.                  Application.EnableVisualStyles();   
  24.                  Application.SetCompatibleTextRenderingDefault(
    false
    );   
  25.                  Application.Run(
    new
    Form1());   
  26.              }   
  27.          }   
  28.         
    /// <summary>
  29.         
    /// 得到已经运行的进程
  30.         
    /// </summary>
  31.         
    /// <returns></returns>
  32.         
    static
    System.Diagnostics.Process GetRunningProcess()   
  33.          {   
  34.              System.Diagnostics.Process theProcess =
    null
    ;   
  35.              System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();    
    //当前进程
  36.              System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();     
    //所有进程
  37.              var myFilename = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace(
    "/"
    , @
    "/"); //得到当前进程名"
  38.             
    foreach
    (var process
    in
    processes)   
  39.              {   
  40.                 
    //查找与当前进程相同名称的进程
  41.                 
    if
    (process.Id != current.Id && process.ProcessName == current.ProcessName)   
  42.                  {   
  43.                     
    //确认相同名称进程的程序运行位置是否一样
  44.                     
    try
  45.                      {   
  46.                         
    if
    (myFilename == process.MainModule.FileName)   
  47.                          {   
  48.                              theProcess = process;    
    //找到了此程序的另一个进程
  49.                          }   
  50.                      }   
  51.                     
    catch
    { }   
  52.                  }   
  53.              }   
  54.             
    return
    theProcess;   
  55.          }   
  56.         
    /*PInvoke 代码(见源程序)*/
  57.      }   
  58. }  

抱歉!评论已关闭.