2013-04-05 143 views
0

我有我已经注册为URI方案一个WPF应用程序通过执行以下操作。注册申请URI方案

HKEY_CLASSES_ROOT 
-->myappname 
    -->shell 
     -->open 
     -->command 
      (Default) = "c:\pathtomyapp\app.exe" 

太棒了!但是,我的应用程序强制一次只能运行一个实例。我如何检测到我的应用程序已在运行,例如将其展示在前台?

+0

敬请展示如何您通过URI方案执行APP.EXE? – YumYumYum 2014-06-30 06:56:43

+1

请考虑修改此问题的标题。它更多的是处理单个实例而不是URI方案处理程序。 – 2015-02-11 10:17:36

回答

2

您可以使用一个名为互斥体检测到应用程序已经运行。或者,如果你有一个GUI应用程序,你可以从VisualBasic's SingleInstance application继承的形式,它会为你做routhgly相同。

public class SingleInstanceController 
    : WindowsFormsApplicationBase 
    { 
    public SingleInstanceController() 
    { 
     // Set whether the application is single instance 
     this.IsSingleInstance = true; 

     this.StartupNextInstance += new 
     StartupNextInstanceEventHandler(this_StartupNextInstance); 
    } 

    void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e) 
    { 
     // Here you get the control when any other instance is 
     // invoked apart from the first one. 
     // You have args here in e.CommandLine. 

     // You custom code which should be run on other instances 
    } 

    protected override void OnCreateMainForm() 
    { 
     // Instantiate your main application form 
     this.MainForm = new Form1(); 
    } 
    } 

[STAThread] 
static void Main(string[] args) 
{  
    SingleInstanceController controller = new SingleInstanceController(); 
    controller.Run(args); 
} 

,只要您在C#编写代码,因为这个类是avaliable作为.NET框架的一个组成部分,所有的语言没关系。

这里是一个wrapper for the WPF

+0

这东西好像被绑定到'WindowsForms'。这是适合在WPF应用程序中使用吗? – Julien 2013-04-05 19:31:17

+0

添加wpf包装链接到答案。 – alex 2013-04-05 19:36:14

+0

这几乎是完美的。不幸的是,使用.NET 4.0时WPF包装器错误 – Julien 2013-04-08 16:32:24