C#只允许运行一个程序实例的多种方法

发布时间:2020-01-08编辑:脚本学堂
本文介绍下,C#编程中只允许一个程序实例运行的多种方法,各有各的不同哦,有需要的朋友参考下吧。

方法1,使用线程互斥变量,通过定义互斥变量来判断是否已运行实例。
把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。已命名的互斥体是系统范围的。

方法2,采用判断进程的方式,在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同程,如是没有运行该程序,如果有就就不运行。
在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("已经运行了一个实例了。");
}
}

方法3,全局原子法,创建程序前,先检查全局原子表中看是否存在特定原子A(创建时添加的),存在时停止创建,说明该程序已运行了一个实例;不存在则运行程序并想全局原子表中添加特定原子A;退出程序时要记得释放特定的原子A哦,不然要到关机才会释放。
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"

以上为大家介绍了,在C#中创建互斥程序基本方法,实现一个程序只能运行一个实例,从代码上来看,第一个相对简单些。
有兴趣的朋友,动手测试下吧。