您的位置:首页 > 其它

如何实现程序只有一个实例

2007-03-13 09:05 671 查看
还是继续参考cnblogs中大虾的文章,

原出处是找不到啦

......

实现了程序的唯一实例

如果打开过程序会有个MessageBox然后将已经打开的程序设为活动窗口

看代码吧

使用VS2005工具

将代码放入Program.cs中

修改其中少部分就可以啦

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Windows.Forms;
usingSystem.Diagnostics;
usingSystem.Runtime.InteropServices;
usingSystem.Threading;
usingSystem.Reflection;
namespaceFxrl
{
staticclassProgram
{
privatestaticMutexmutex=null;
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#region方法一
if(CreateMutex())
{
Application.Run(newForm3());
//设置启动的窗口

ReleasMutex();
}
else
{
MessageBox.Show("程序已运行");
HandleRunningInstance();
}
#endregion
#region方法二
//Processp=GetRunningInstance();
//if(p!=null)//已经有应用程序副本执行
//{
//HandleRunningInstance(p);
//}
//else//启动第一个应用程序
//{
//Application.Run(newMainForm());
//}
////简洁的调用为,
////if(HandleRunningInstance()==false)
////{
////Application.Run(newFrmUpdate());
////}
#endregion
}
staticboolCreateMutex()
{
//returnCreateMutex(Assembly.GetEntryAssembly().FullName);
returnCreateMutex(AssemblyProduct+AssemblyProduct);
}
staticboolCreateMutex(stringname)
{
boolresult=false;
mutex=newMutex(true,name,outresult);
returnresult;
}
staticvoidReleasMutex()
{
if(mutex!=null)
mutex.Close();
}
//使用GetRunningInstance静态方法获取应用程序进程实例,如果没有匹配进程,返回Null值,
publicstaticProcessGetRunningInstance()
{
ProcesscurrentProcess=Process.GetCurrentProcess();//获取当前进程
//获取当前运行程序完全限定名
stringcurrentFileName=currentProcess.MainModule.FileName;
//获取进程名为ProcessName的Process数组。
Process[]processes=Process.GetProcessesByName(currentProcess.ProcessName);
//遍历有相同进程名称正在运行的进程
foreach(Processprocessinprocesses)
{
if(process.MainModule.FileName==currentFileName)
{
if(process.Id!=currentProcess.Id)//根据进程ID排除当前进程
returnprocess;//返回已运行的进程实例
}
}
returnnull;
}
[DllImport("User32.dll")]
privatestaticexternboolShowWindowAsync(IntPtrhWnd,intcmdShow);
[DllImport("User32.dll")]//前端显示窗体
privatestaticexternboolSetForegroundWindow(IntPtrhWnd);
//定义类成员辅助变量,
privateconstintWS_SHOWNORMAL=1;
//以上的方法声明为私有,对其进一步包装,HandleRunningInstance静态方法
//为获取应用程序句柄,设置应用程序为前台运行,并返回bool值。
publicstaticboolHandleRunningInstance(Processinstance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle,WS_SHOWNORMAL);
//设置为foregroundwindow
returnSetForegroundWindow(instance.MainWindowHandle);
}
//对上面的方法创建一个重载版本,使调用代码更加简洁
publicstaticboolHandleRunningInstance()
{
Processp=GetRunningInstance();
if(p!=null)
{
HandleRunningInstance(p);
returntrue;
}
returnfalse;
}
#region属性(返回程序集的产品名及公司名)
publicstaticstringAssemblyProduct
{
get
{
//获取此程序集上的所有Product属性
object[]attributes=Assembly.GetExecutingAssembly().GetCustomAttributes(
typeof(AssemblyProductAttribute),false);
//如果Product属性不存在,则返回一个空字符串
if(attributes.Length==0)
return"";
//如果有Product属性,则返回该属性的值
return((AssemblyProductAttribute)attributes[0]).Product;
}
}
publicstaticstringAssemblyCompany
{
get
{
//获取此程序集上的所有Company属性
object[]attributes=Assembly.GetExecutingAssembly().GetCustomAttributes(
typeof(AssemblyCompanyAttribute),false);
//如果Company属性不存在,则返回一个空字符串
if(attributes.Length==0)
return"";
//如果有Company属性,则返回该属性的值
return((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
#endregion
}
}

.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
章节导航