C# 程序只运行一次的处理方法

方法一:遍历正在有相同名字运行的例程
 1  using  System;   
 2  using  System.Collections.Generic;   
 3  using  System.Text;   
 4  using  System.Diagnostics;   
 5  using  System.Runtime.InteropServices;   
 6  namespace  DHPSS.COM   
 7  {   
 8       ///   <summary>    
 9       ///  c#程序只运行一次,C#例程只运行一次   
10       ///   http://blog.csdn.net/nnsword    
11       ///  QQ:16349023   
12       ///   </summary>    
13       public   class  CHandleRunningPro   
14      {   
15           ///     <summary>      
16           ///    const   int,   ShowWindowAsync   使用时的参数,让窗体正常显示确保没有隐藏和最小化。     
17           ///     </summary>      
18           private   const   int  WS_SHOWNORMAL  =   1 ;   
19           ///     <summary>      
20           ///    功能:遍历所有运行的例程,判断是否已经有相同名字的例程。                   
21           ///    实现:遍历所有运行的例程,如果有与自己名字相同的比较   ID   是否相同。     
22           ///         如果ID不同说明已经有实例在运行,则把该实例返回。     
23           ///         如果没有则返回   null     
24           ///     </summary>      
25           ///     <returns> 有相同名字的例程返回其   process,否则返回null </returns>      
26           private  Process GetRunningInstance()   
27          {   
28              Process current  =  Process.GetCurrentProcess();   
29              Process[] processes  =  Process.GetProcessesByName(current.ProcessName);   
30               // 遍历正在有相同名字运行的例程     
31               foreach  (Process process  in  processes)   
32              {   
33                   // 忽略现有的例程     
34                   if  (process.Id  !=  current.Id)   
35                  {   
36                       return  process;   
37                  }   
38              }   
39               // 没有其它的例程,返回Null     
40               return   null ;   
41          }   
42           // 接下来调用两个WinAPI,其功能将在包装方法中描述,       
43          [DllImport( " User32.dll " )]   
44           private   static   extern   bool  ShowWindowAsync(IntPtr hWnd,  int  cmdShow);   
45          [DllImport( " User32.dll " )]   
46           private   static   extern   bool  SetForegroundWindow(IntPtr hWnd);   
47           // 以上的方法声明为私有,对其进一步包装,     
48           ///     <summary>      
49           ///    功能:HandleRunningInstance静态方法为获取应用程序句柄,设置应用程序为前台运行,并返回bool值。                 
50           ///    实现:确保窗口没有被最小化或最大化。     
51           ///                设置为前台显示。     
52           ///     </summary>      
53           ///     <param   name="instance"> 准备设置成前台运行的程序 </param>      
54           ///     <returns></returns>      
55           private   bool  HandleRunningInstance(Process instance)   
56          {   
57               // 确保窗口没有被最小化或最大化       
58              ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);   
59               // 设置为foreground   window       
60               return  SetForegroundWindow(instance.MainWindowHandle);   
61          }   
62           ///     <summary>      
63           ///    功能:查找相同名字的进程,有则前台显示,无则返回   false     
64           ///    实现:1   查找相同名字的进程。     
65           ///         2   有相同名字的进程,将其前台显示,返回   true     
66           ///         3   无相同名字,返回   false     
67           ///     </summary>      
68           ///     <returns> 有相同名字   true,   无相同名字   false </returns>      
69           public   bool  HandleRunningInstance()   
70          {   
71              Process p  =  GetRunningInstance();   
72               if  (p  !=   null )   
73              {   
74                  HandleRunningInstance(p);   
75                   return   true ;   
76              }   
77               return   false ;   
78          }       
79      }   
80  }  

方法二:通过同步进程实例,看其是否已经创建
 1  修改program.cs中的程序   
 2  *********************************************    
 3  using  System;   
 4  using  System.Collections.Generic;   
 5  using  System.Windows.Forms;   
 6  using  System.Threading;   
 7  namespace  PCBilling_Server   
 8  {   
 9       static   class  Program   
10      {   
11           public   static   bool  bOnlyOneInstance  =   false ;   
12           ///   <summary>    
13           ///  应用程序的主入口点。   
14           ///   </summary>    
15          [STAThread]   
16           static   void  Main()   
17          {   
18              bOnlyOneInstance  =   false ;   
19              Mutex mutex  =   new  Mutex( true , Application.UserAppDataPath.Replace( @" \ " " _ " ),  out  bOnlyOneInstance);   
20               if  ( ! bOnlyOneInstance)   
21              {   
22                  MessageBox.Show( " 系统已经运行! " " 提示信息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);   
23                  System.Environment.Exit( 0 );   
24                   return ;   
25              }   
26              Application.EnableVisualStyles();   
27              Application.SetCompatibleTextRenderingDefault( false );   
28              Application.Run( new  Frm_Login());   
29          }   
30      }   
31 



转载于:https://www.cnblogs.com/uhome/archive/2011/06/02/2068441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值