我最终参考的例子,可以下了用vs打开看
http://download.csdn.net/source/1858862
解决方案原文摘抄如下:
You could use the Mutex class, but you will soon find out that you will need to implement the code to pass the arguments and such yourself. Well, I learned a trick when programming in WinForms when I read Chris Sell's book. This trick uses logic that is already available to us in the framework. I don't know about you, but when I learn about stuff I can reuse in the framework, that is usually the route I take instead of reinventing the wheel. Unless of course it doesn't do everything I want.
When I got into WPF, I came up with a way to use that same code, but in a WPF application. This solution should meet your needs based off your question.
First, we need to create our application class. In this class we are going override the OnStartup event and create a method called Activate, which will be used later.
public class SingleInstanceApplication : System.Windows.Application
{
protected override void OnStartup(System.Windows.StartupEventArgs e)
{
// Call the OnStartup event on our base class
base.OnStartup(e);
// Create our MainWindow and show it
MainWindow window = new MainWindow();
window.Show();
}
public void Activate()
{
// Reactivate the main window
MainWindow.Activate();
}
}
Second, we will need to create a class that can manage our instances. Before we go through that, we are actually going to reuse some code that is in the Microsoft.VisualBasic assembly. Since, I am using C# in this example, I had to make a reference to the assembly. If you are using VB.NET, you don't have to do anything. The class we are going to use is WindowsFormsApplicationBase and inherit our instance manager off of it and then leverage properties and events to handle the single instancing.
public class SingleInstanceManager : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
private SingleInstanceApplication _application;
private System.Collections.ObjectModel.ReadOnlyCollection<string> _commandLine;
public SingleInstanceManager()
{
IsSingleInstance = true;
}
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
{
// First time _application is launched
_commandLine = eventArgs.CommandLine;
_application = new SingleInstanceApplication();
_application.Run();
return false;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
// Subsequent launches
base.OnStartupNextInstance(eventArgs);
_commandLine = eventArgs.CommandLine;
_application.Activate();
}
}
Basically, we are using the VB bits to detect single instance's and process accordingly. OnStartup will be fired when the first instance loads. OnStartupNextInstance is fired when the application is re-run again. As you can see, I can get to what was passed on the command line through the event arguments. I set the value to an instance field. You could parse the command line here, or you could pass it to your application through the constructor and the call to the Activate method.
Third, it's time to create our EntryPoint. Instead of newing up the application like you would normally do, we are going to take advantage of our SingleInstanceManager.
public class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
SingleInstanceManager manager = new SingleInstanceManager();
manager.Run(args);
}
}
Well, I hope you are able to follow everything and be able use this implementation and make it your own.
Important link:
最重要的,有很多代码 讨论和资源
里面有两种比较好的解决方案
What is the correct way to create a single instance application?
http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application
微软的专题和例子
Single Instance Detection Sample
http://msdn.microsoft.com/en-us/library/ms771662.aspx
Sample download
http://download.microsoft.com/download/f/6/e/f6e32974-726e-4054-96af-9c747bf89a6e/SingleInstanceDetectionSample.exe
带有很多说明性文字
WPF - Writing a Single Instance Application
http://www.switchonthecode.com/tutorials/wpf-writing-a-single-instance-application
中文 博客园文章 微软例子的翻译版
WPF Single Instance workaround
http://www.cnblogs.com/dc10101/archive/2009/11/24/1609146.html
包含一种不好的方法,找进程名
Single Instance WPF Application in .NET 3.x
http://pietschsoft.com/post/2009/01/Single-Instance-WPF-Application-in-NET-3.aspx
codeplex项目 我没用上
WPF instance aware application (for single instance applications)
http://www.codeplex.com/wpfinstanceawareapp
分享到:
相关推荐
This sample demonstrates how to implement single instance detection for standalone applications.
1、使用用互斥量(System.Threading.Mutex) 2、将以运行实例窗体置顶
1、Prism各个重要概念实例 2、MVVM架构 3、WPF中使用Prism
WPF实例(实例比较多) (wpf窗体继承)RibbonStyle2.zip (赛车游戏)GrandPrix_SRC.zip CustomRulesMVVM.zip ExifCompareSource.zip Explorer3D.zip familyshow-12528.zip Jigsaw.zip Life_src.zip PhotoBooth.zip ...
wpf学习实例.rarwpf学习实例.rarwpf学习实例.rarwpf学习实例.rarwpf学习实例.rarwpf学习实例.rar
这是一个通过WPF Frame控件实现多个Page之间的导航功能实例
WPF实现的一个不规则窗体的实例,WPF实现的一个不规则窗体的实例,
WPF 入门教程 + 一系列源码实例 Wpf_3D立方体实例 WPF_MouseWheel事件实例 WPF_TextBlock实例 Wpf_路由事件实例 WPF命令与文件保存实例
一个wpf+mvvm的实例
WPF的全称是Windows Presentation Foundation,是Vista操作系统的三大核心开发库之一,它运行在.NET Framework 3.0以上框架下,为用户界面、2D/3D 图形、文档和媒体提供了统一的描述和操作方法。WPF是基于DirectX...
Winform窗体只允许打开一个实例。第二次打开报错。
C#WPF 刷新窗口 实例 可运行 http://www.cnblogs.com/leco/archive/2010/11/28/1890046.html WPF中实现Timer 事件 在 WPF 中不再有类似 WinForm 中的 Timer 控件,因此,需要使用 DispatcherTimer 类来实现类似 ...
wpf技术开发编程实例,wpf技术开发编程实例wpf技术开发编程实例
WPF设计的播放器实例,更好地去学习WPF开发
WPF实例 SDK 3.5中的实例,全部可以运行
WPF实例(实例比较多) (wpf窗体继承)RibbonStyle2.zip (赛车游戏)GrandPrix_SRC.zip CustomRulesMVVM.zip ExifCompareSource.zip Explorer3D.zip familyshow-12528.zip Jigsaw.zip Life_src.zip PhotoBooth.zip ...
主要介绍了WPF的ListView控件自定义布局的方法,结合实例形式分析了WPF中ListView控件的布局方法,需要的朋友可以参考下
Telerik WPF实例源码Telerik WPF实例源码Telerik WPF实例源码Telerik WPF实例源码Telerik WPF实例源码Telerik WPF实例源码Telerik WPF实例源码
基于 Microsoft.VisualBasic.dll
wpf 教程 源码 wpf 实例源码 好东西