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

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

问题描述

在.NET中,什么是从在同一时间运行的应用程序的prevent多个实例的最好方法?如果没有最好的技术,有哪些告诫与每个解决方案来考虑的?

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:

<一个href="http://odeto$c$c.com/Blogs/scott/archive/2004/08/20/401.aspx">http://odeto$c$c.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";

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

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