我有我已经注册为URI方案一个WPF应用程序通过执行以下操作。注册申请URI方案
HKEY_CLASSES_ROOT
-->myappname
-->shell
-->open
-->command
(Default) = "c:\pathtomyapp\app.exe"
太棒了!但是,我的应用程序强制一次只能运行一个实例。我如何检测到我的应用程序已在运行,例如将其展示在前台?
我有我已经注册为URI方案一个WPF应用程序通过执行以下操作。注册申请URI方案
HKEY_CLASSES_ROOT
-->myappname
-->shell
-->open
-->command
(Default) = "c:\pathtomyapp\app.exe"
太棒了!但是,我的应用程序强制一次只能运行一个实例。我如何检测到我的应用程序已在运行,例如将其展示在前台?
您可以使用一个名为互斥体检测到应用程序已经运行。或者,如果你有一个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
敬请展示如何您通过URI方案执行APP.EXE? – YumYumYum 2014-06-30 06:56:43
请考虑修改此问题的标题。它更多的是处理单个实例而不是URI方案处理程序。 – 2015-02-11 10:17:36