MVP? MVP!

i love C#
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

防止同一程序多次运行

Posted on 2009-02-11 10:34  renhb  阅读(441)  评论(0)    收藏  举报
[STAThread]
public static void Main()
{
   
bool ret;
    System.Threading.Mutex mutex
= new System.Threading.Mutex(true, Application.ProductName, out ret);
   
if (ret)
    {
        System.Windows.Forms.Application.EnableVisualStyles();  
//这两行实现   XP   可视风格  
         System.Windows.Forms.Application.DoEvents();
       
//   Main   为你程序的主窗体,如果是控制台程序不用这句
         System.Windows.Forms.Application.Run(new Main());  
         mutex.ReleaseMutex();
    }
   
else
    {
        MessageBox.Show(
null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
       
//   提示信息,可以删除。  
        Application.Exit();//退出程序  
    }
}