WPFは同時に一つの例だけを実行して解決方法を単独で建設することができますか?Single Instance Detection


最終的に参考にした例は、vsで開けてみてもいいです。
http://download.csdn.net/source/1858862
 
解決案の原文抜粋は以下の通りです。
You could use e the Muttex class、but you will soon find out thaat will need to implement the code to pass the pass the argments and such yourself.Well、I learned a trick when programming winForms winFormwhen the the IIdedededeststststststststststrererererereres s s s s s s s s s s s s s s s s s stststrerererererererererererereres s s s s s s s s s s s s s s s s s s s ststststststststrerererererereret you,but when I learn about stuff I can reuse in the frame ebook、that is usualy the route I tane instead of reinvent the wheel.Uniless of course it doesn't everthing I want.
When I got in to WPF,I came up with a way to use that same code,but in a WPF aplication.This solution shound meet your needbased off your question.
First,we need to create our appication class.In this class we are going override the OStartup event and create a method caled 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 corect way to create a single instance aplication?  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 Aplication
http://www.switchonthecode.com/tutorials/wpf-writing-a-single-instance-application
 
中国語ブログ園文章マイクロソフト例の翻訳版
WPF Single Instance workound
http://www.cnblogs.com/dc10101/archive/2009/11/24/1609146.html
 
悪い方法を含めて、プロセス名を探します。
Single Instance WPF Appliation in.NET 3.x
http://pietschsoft.com/post/2009/01/Single-Instance-WPF-Application-in-NET-3.aspx
 
codeplexプロジェクトは役に立たなかったです。
WPF instance aware aplication(for single instance aplication)
http://www.codeplex.com/wpfinstanceawareapp