C# 防止.NET 中给定应用程序的多个实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/93989/
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
Prevent multiple instances of a given app in .NET?
提问by C. Dragon 76
In .NET, what's the best way to prevent multiple instances of an app from running at the same time? And if there's no "best" technique, what are some of the caveats to consider with each solution?
在 .NET 中,防止一个应用程序的多个实例同时运行的最佳方法是什么?如果没有“最佳”技术,那么每种解决方案需要考虑哪些注意事项?
采纳答案by ImJustPondering
Use Mutex. One of the examples above using GetProcessByName has many caveats. Here is a good article on the subject:
使用互斥锁。上面使用 GetProcessByName 的示例之一有很多注意事项。这是一篇关于这个主题的好文章:
http://odetocode.com/Blogs/scott/archive/2004/08/20/401.aspx
http://odetocode.com/Blogs/scott/archive/2004/08/20/401.aspx
[STAThread]
static void Main()
{
using(Mutex mutex = new Mutex(false, "Global\" + appGuid))
{
if(!mutex.WaitOne(0, false))
{
MessageBox.Show("Instance already running");
return;
}
Application.Run(new Form1());
}
}
private static string appGuid = "c0a76b5a-12ab-45c5-b9d9-d693faa6e7b9";
回答by Aaron Jensen
http://en.csharp-online.net/Application_Architecture_in_Windows_Forms_2.0—Single-Instance_Detection_and_Management
http://en.csharp-online.net/Application_Architecture_in_Windows_Forms_2.0—Single-Instance_Detection_and_Management
回答by Jorge Ferreira
This article simply explains how you can create a windows application with control on the number of its instances or run only single instance. This is very typical need of a business application. There are already lots of other possible solutions to control this.
本文简单地解释了如何创建 Windows 应用程序,并控制其实例数或仅运行单个实例。这是业务应用程序非常典型的需求。已经有很多其他可能的解决方案来控制这一点。
http://www.openwinforms.com/single_instance_application.html
http://www.openwinforms.com/single_instance_application.html
回答by bdukes
回答by tim_yates
回答by Thomas Jespersen
You have to use System.Diagnostics.Process.
您必须使用 System.Diagnostics.Process。
Check out: http://www.devx.com/tips/Tip/20044
回答by Tomer Gabel
Normally it's done with a named Mutex (use new Mutex( "your app name", true ) and check the return value), but there's also some support classes in Microsoft.VisualBasic.dll that can do it for you.
通常它是用一个命名的互斥体完成的(使用 new Mutex("your app name", true ) 并检查返回值),但 Microsoft.VisualBasic.dll 中也有一些支持类可以为你完成。
回答by Thomas Wagner
if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
{
AppLog.Write("Application XXXX already running. Only one instance of this application is allowed", AppLog.LogMessageType.Warn);
return;
}
回答by Doliveras
Using Visual Studio 2005 or 2008 when you create a project for an executable, on the properties windows inside the "Application" panel there is a check box named “Make single instance application” that you can activate to convert the application on a single instance application.
使用 Visual Studio 2005 或 2008 为可执行文件创建项目时,在“应用程序”面板内的属性窗口中有一个名为“制作单实例应用程序”的复选框,您可以激活该复选框以在单实例应用程序上转换应用程序.
Here is a capture of the window I'm talking of:
This is a Visual Studio 2008 windows application project.
这是我所说的窗口的捕获:
这是一个 Visual Studio 2008 windows 应用程序项目。
回答by C. Dragon 76
It sounds like there are 3 fundamental techniques that have been suggested so far.
听起来到目前为止已经提出了 3 种基本技术。
- Derive from the Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase class and set the IsSingleInstance property to true. (I believe a caveat here is that this won't work with WPF applications, will it?)
- Use a named mutex and check if it's already been created.
- Get a list of running processes and compare the names of the processes. (This has the caveat of requiring your process name to be unique relative to any other processes running on a given user's machine.)
- 从 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase 类派生,并将 IsSingleInstance 属性设置为 true。(我相信这里需要注意的是,这不适用于 WPF 应用程序,是吗?)
- 使用命名的互斥锁并检查它是否已经创建。
- 获取正在运行的进程列表并比较进程的名称。(这有一个警告,即要求您的进程名称相对于在给定用户机器上运行的任何其他进程是唯一的。)
Any caveats I've missed?
我错过了任何警告?