`
lovnet
  • 浏览: 6705013 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

WPF同时只允许一个实例运行解决方法 单建? Single Instance Detection

阅读更多

我最终参考的例子,可以下了用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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics