您的位置:首页 > 其它

WPF:如何实现单实例的应用程序(Single Instance)

2010-03-25 09:44 447 查看
好吧,这是我将WPF与WindowsForms进行比较的系列文章的第四篇,讨论一下如何实现单实例(singleinstance)

先来看第一种最简单粗暴的做法:

检测进程名,如果名称一样,则表示程序已经启动了,就不再启动.

protectedoverridevoidOnStartup(StartupEventArgse)
{
//GetReferencetothecurrentProcess
ProcessthisProc=Process.GetCurrentProcess();
//Checkhowmanytotalprocesseshavethesamenameasthecurrentone
if(Process.GetProcessesByName(thisProc.ProcessName).Length>1)
{
//Iftherismorethanone,thanitisalreadyrunning.
MessageBox.Show("Applicationisalreadyrunning.");
Application.Current.Shutdown();
return;
}

base.OnStartup(e);
}
很简单,不是吗?但简单有什么错呢?它很实用.
[注意]这个代码如果在visualstudio中调试则无效,因为visualstudio调试用的进程是加了一个vshost的后缀的。
第二种方案我觉得应该还是可以用mutex来实现嘛,看看下面的代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Windows;
usingSystem.Diagnostics;
usingSystem.Threading;

namespaceWpfApplication1
{
///<summary>
///App.xaml的交互逻辑
///</summary>
publicpartialclassApp:Application
{
protectedoverridevoidOnStartup(StartupEventArgse)
{
boolcreateNew;
Mutexmutex=newMutex(true,"MyApplication",outcreateNew);
if(createNew)
base.OnStartup(e);
else
{
MessageBox.Show("程序已经启动了");
Application.Current.Shutdown();
}
}

}
}


.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;}

这一种做法的结果与第一种很类似,或者说没有任何区别。

看起来解决问题了,但仍然不是很理想的。最好的情况是,当用户开启第二个实例的时候,如果第一个实例没有处于活动状态,则应该激活它。

我们很自然还是联想到了原先在WindowsForms时代的WindowsFormsApplicationBase,那里面做这个事情太简单了。

首先,添加Microsoft.VisualBasic的引用





namespaceWpfApplication1
{
publicclassEntryPoint
{
[STAThread]
publicstaticvoidMain(string[]args)
{
SingleInstanceManagermanager=newSingleInstanceManager();
manager.Run(args);
}
}

//UsingVBbitstodetectsingleinstancesandprocessaccordingly:
//*OnStartupisfiredwhenthefirstinstanceloads
//*OnStartupNextInstanceisfiredwhentheapplicationisre-runagain
//NOTE:itisredirectedtothisinstancethankstoIsSingleInstance
publicclassSingleInstanceManager:WindowsFormsApplicationBase
{
SingleInstanceApplicationapp;

publicSingleInstanceManager()
{
this.IsSingleInstance=true;
}

protectedoverrideboolOnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgse)
{
//Firsttimeappislaunched
app=newSingleInstanceApplication();
app.Run();
returnfalse;
}

protectedoverridevoidOnStartupNextInstance(StartupNextInstanceEventArgseventArgs)
{
//Subsequentlaunches
base.OnStartupNextInstance(eventArgs);
app.Activate();
}
}

publicclassSingleInstanceApplication:Application
{
protectedoverridevoidOnStartup(System.Windows.StartupEventArgse)
{
base.OnStartup(e);

//Createandshowtheapplication'smainwindow
//MainWindowwindow=newMainWindow();
Window1window=newWindow1();
window.Show();
}

publicvoidActivate()
{
//Reactivateapplication'smainwindow
this.MainWindow.Show();
this.MainWindow.Activate();
}
}
}

.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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐