WinForm高级编程之防止多开

        用户点多少次【.exe】文件,就会执行多少次程序,这是我们不希望看到的,本文就介绍防止此类情况的解决代码。

在应用程序运行前加上判断控制代码(蓝色字体部分),完整代码如下:

 

using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;
 
namespace SunWayFortune.Hospital.SWHIS {    
 
 static class Program     {        
 
 /// <summary>       
 
  /// 应用程序的主入口点。     
 
    /// </summary>   
 
      [STAThread]         static void Main()    
 
     {             //获取当前进程的一个伪句柄        
 
     System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
 
            //获取包含当前进程的一个列表         
 
     System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcessesByName(currentProcess.ProcessName);
 
            //如果前进程已经存在         
 
    if (processList.Length > 1)        
 
     {           
 
       //提示信息            
 
     MessageBox.Show("当前的应用程序已经被打开!", "提示", MessageBoxButtons.OK);
 
                //退出以下所有操作            
 
      return;        
 
     }
 
            Application.EnableVisualStyles();         
 
    Application.SetCompatibleTextRenderingDefault(false);
 
            //创建登录画面     
 
        FrmLogin li = new FrmLogin();
 
            //显示登录画面       
 
      li.ShowDialog();
 
            //判断登录是否成功       
 
      if (li.DialogResult.Equals(DialogResult.OK))         
 
    {              
 
   //登录成功时            
 
     //关闭登录画面        
 
         li.Close();              
 
   //显示主窗体           
 
      Application.Run(new FrmMdi());    
 
         }     
 
    }   
 
  }
 
}


欢迎大家收藏,转载请注明出处:电脑技术分享

posted @ 2012-04-14 10:35  龙铭洪官方网站  阅读(407)  评论(0编辑  收藏  举报