解决C#程序只允许运行一个实例的几种方法详解

方法一:
使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.
把program.cs文件里的Main()函数改为如下代码:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace NetTools
{
    static class Program
    {
        [DllImport("user32.dll")]
        private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
        [DllImport("user32.dll")]
        private static extern bool FlashWindowEx(int pfwi);
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);
            if (runone)
            {
                run.ReleaseMutex();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                FrmRemote frm = new FrmRemote();
                int hdc = frm.Handle.ToInt32(); // write to ...
                Application.Run(frm);
                IntPtr a = new IntPtr(hdc);
            }
            else
            {
                MessageBox.Show("已经运行了一个实例了。");
                //IntPtr hdc = new IntPtr(1312810); // read from...
                //bool flash = FlashWindow(hdc, true);
            }
        }
    }
}

说明:程序中通过语句 System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);来创建一个互斥体变量run,其中"single_test"为互斥体名,在此方法返回时,如果创建了局部互斥体或指定的命名系统互斥体,则布尔值runone为true;如果指定的命名系统互斥体已存在,则为 false。已命名的互斥体是系统范围的。
方法二:采用判断进程的方式,我们在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同程,如是没有运行该程序,如果有就就不运行.在C#中应用System.Diagnostics名字空间中的Process类来实现,主要代码如下: 
1,在program.cs文件中添加函数如下:

public static System.Diagnostics.Process RunningInstance() 
{ 
System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess(); 
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses(); 
foreach (System.Diagnostics.Process process in processes) //查找相同名称的进程 
{ 
if (process.Id != current.Id) //忽略当前进程 
{ //确认相同进程的程序运行位置是否一样. 
if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @"/") == current.MainModule.FileName) 
  { //Return the other process instance. 
   return process; 
  } 
} 
} //No other instance was found, return null. 
return null; 
} 

2,把Main ()函数改为如下代码:

static void Main() 
{ 
if(RunningInstance()==null) 
{ 
Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false); 
Application.Run(new Form1()); 
} 
else 
{ 
MessageBox.Show("已经运行了一个实例了。"); 
} 
} 

方法三:全局原子法,创建程序前,先检查全局原子表中看是否存在特定原子A(创建时添加的),存在时停止创建,说明该程序已运行了一个实例;不存在则运行程序并想全局原子表中添加特定原子A;退出程序时要记得释放特定的原子A哦,不然要到关机才会释放。C#实现如下: 
1.申明WinAPI函数接口

[System.Runtime.InteropServices.DllImport("kernel32.dll")] 
public static extern UInt32 GlobalAddAtom(String lpString); //添加原子 
[System.Runtime.InteropServices.DllImport("kernel32.dll")] 
public static extern UInt32 GlobalFindAtom(String lpString); //查找原子 
[System.Runtime.InteropServices.DllImport("kernel32.dll")] 
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); //删除原子

2.修改Main()函数如下:

static void Main() 
{ 
if (GlobalFindAtom("jiaao_test") == 77856768) //没找到原子"jiaao_test" 
{ 
GlobalAddAtom("jiaao_test"); //添加原子"jiaao_test" 
Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false); 
Application.Run(new Form1()); 
} 
else 
{ 
MessageBox.Show("已经运行了一个实例了。"); 
} 
} 

3.在FormClosed事件中添加如下代码: 
GlobalDeleteAtom(GlobalFindAtom("jiaao_test"));//删除原子"jiaao_test" 
--------------------------------------*-------*--------*-----------------------------------------------
以上为创建互斥程序的基本通用的思想,个人认为,第一种方法最简单。

posted @ 2016-04-18 11:56  zhuweirandy  阅读(9772)  评论(0编辑  收藏  举报