WPF 只强制一个应用程序实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/414642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Enforce only ONE instance of application
提问by John Batdorf
How do I allow only one instance of a WPF application to run?
如何只允许运行 WPF 应用程序的一个实例?
Thanks.
谢谢。
回答by sobelito
http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx
http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx
Doesn't require VB.DLL as some other examples advise. Has WPF sample code. Passes any cmd line args to initial instance.
不需要 VB.DLL 作为一些其他示例建议。有 WPF 示例代码。将任何 cmd 行参数传递给初始实例。
回答by Artur Carvalho
Try this: Single instance application. Ive used the second method and it works fine.
试试这个:单实例应用程序。我用过第二种方法,效果很好。
回答by VoteCoffee
I use this helper method and call it from the application.startup event
我使用这个辅助方法并从 application.startup 事件中调用它
Public Sub ForceSingleInstanceApplication()
'Get a reference to the current process
Dim MyProc As Process = Process.GetCurrentProcess
'Check how many processes have the same name as the current process
If (Process.GetProcessesByName(MyProc.ProcessName).Length > 1) Then
'If there is more than one, it is already running
MsgBox("Application is already running", MsgBoxStyle.Critical, My.Application.Info.Title) 'Reflection.Assembly.GetCallingAssembly().GetName().Name)
' Terminate this process and give the operating system the specified exit code.
Environment.Exit(-2)
Exit Sub
End If
End Sub
回答by Sergio Basurco
User sobelito
linked thispost, which has the following update. What it says is that for an updated resource you should use Windows 7 Taskbar Single Instance, which if you look into the source will allow you to do what you need.
用户sobelito
链接了这篇文章,其中有以下更新。它说的是,对于更新的资源,您应该使用Windows 7 Taskbar Single Instance,如果您查看源代码,它将允许您执行所需的操作。
You can use the SingleInstance
c# project. It also contains samples for both WinForms and WPF.
您可以使用SingleInstance
c# 项目。它还包含 WinForms 和 WPF 的示例。
Note that it's also released under the Apache 2.0 license, unlike Arik's Poznanski post in the Microsoft Blog, which is (IANAL, AFAIK) not commercially available.
请注意,它也是在 Apache 2.0 许可下发布的,这与 Arik 在 Microsoft 博客中的 Poznanski 帖子不同,后者(IANAL,AFAIK)未在商业上销售。
回答by jsirr13
Check out this solution: Allowing only one instance of a WPF application to execute
查看此解决方案:仅允许执行 WPF 应用程序的一个实例
This not only enforces one instance of an application, but it also gives your current application focus when an additional instance of an application is ran. My mutex solution to restricting one instance is actually different from the above link, but I liked the "focus" element to this solution.
这不仅强制应用程序的一个实例,而且在运行应用程序的其他实例时,它还为您当前的应用程序提供焦点。我限制一个实例的互斥锁解决方案实际上与上面的链接不同,但我喜欢这个解决方案的“焦点”元素。