防止.NET 中给定应用程序的多个实例? [英] Prevent multiple instances of a given app in .NET?

查看:31
本文介绍了防止.NET 中给定应用程序的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET 中,防止一个应用程序的多个实例同时运行的最佳方法是什么?如果没有最佳"技术,那么每种解决方案需要考虑哪些注意事项?

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?

推荐答案

使用互斥锁.上面使用 GetProcessByName 的示例之一有很多注意事项.这是一篇关于这个主题的好文章:

Use Mutex. One of the examples above using GetProcessByName has many caveats. Here is a good article on the subject:

http://odetocode.com/Blogs/scott/存档/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";

这篇关于防止.NET 中给定应用程序的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆