WPF:如何实现单实例的应用程序(Single Instance)
2010-03-25 09:44
447 查看
好吧,这是我将WPF与WindowsForms进行比较的系列文章的第四篇,讨论一下如何实现单实例(singleinstance)
先来看第一种最简单粗暴的做法:
检测进程名,如果名称一样,则表示程序已经启动了,就不再启动.
.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的引用
.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;}
先来看第一种最简单粗暴的做法:
检测进程名,如果名称一样,则表示程序已经启动了,就不再启动.
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;}
相关文章推荐
- WPF:如何实现单实例的应用程序(Single Instance)
- WPF(四)Application3:如何实现单实例的应用程序(Single Instance)
- WPF:如何实现单实例的应用程序(Single Instance)
- WPF:如何实现单实例的应用程序(Single Instance)
- WPF:如何实现单实例的应用程序(Single Instance)
- C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)
- 实现单进程(Single Instance)的.NET WinForm应用程序
- 实现单进程(Single Instance)的.NET WinForm应用程序
- WPF学习笔记 - 如何用WPF创建单实例应用程序
- WPF:实现主应用程序单一实例运行方式总结
- WPF实现单进程实例应用程序(像Word一样可以打开多个文件)
- WPF学习笔记 - 如何用WPF创建单实例应用程序
- 使用 Python 和 Oracle 数据库实现高并发性 了解如何借助线程和并发性提升支持 Oracle 数据库的 Python 应用程序的吞吐量和响应性。
- 自定义类于Main方法中实现对WPF应用程序的启动
- 如何利用局域网来实现VLAN的实例
- 如何利用jquery实现一句话全选/取消全选的实例代码
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- 如何在 ASP.NET 应用程序中实现模拟
- 如何使应用程序只有一个实例在运行
- 基于.Net平台应用程序唯一运行实例实现(一)