文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>让Windows窗体只运行一次,并在第二次启动窗体时激活该窗体

让Windows窗体只运行一次,并在第二次启动窗体时激活该窗体

时间:2010-12-14  来源:望远镜


方法二:
Mutex 检测冲突  1 
 2         [STAThread]
 3         static void Main(string[] args)
 4         {
 5             bool isFirst;
 6 
 7             System.Threading.Mutex mutex = new System.Threading.Mutex(true, "WindowAppTest", out isFirst);
 8             //这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突 
 9             if (!isFirst)
10             {
11                 MessageBox.Show("Exist");
12                 Environment.Exit(1);//实例已经存在,退出程序 
13             }
14             else
15             {
16                 Application.Run(new Form1());
17             }
18         } 
19 

 


方法三
把AssemblyInfo.cs里的[assembly: AssemblyFileVersion("1.0.0.0")]改为[assembly:AssemblyFileVersion("2.0.0.8")],然后利用该信息进行判断。
代码如下:
程序集版本号  1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Diagnostics;
 6 using System.Reflection;
 7 using System.Collections;
 8 using System.Threading;
 9 
10 namespace MyWork_01
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             Process[] processes = Process.GetProcesses(); //获得当前所有进程 
17             Process currentProcess = Process.GetCurrentProcess(); //获取当前正在运行进程 
18             ProcessModule currentPM = currentProcess.Modules[0];
19             int same = 0; //相同运行实例个数 
20             ArrayList proList = new ArrayList(); //将相同实例加入此集合中 
21 
22             foreach (Process p in processes)
23             {
24                 try//由于进程不同,有的进程不包含Modules信息,所以要用try保护 
25                 {
26                     if (p.Modules != null)
27                         if (p.Modules.Count > 0)
28                         {
29                             System.Diagnostics.ProcessModule pm = p.Modules[0];
30                             if (pm.FileVersionInfo.FileVersion.Equals(currentPM.FileVersionInfo.FileVersion))
31                             {
32                                 same++;
33                                 proList.Add(p);
34                             }
35                             if (same > 1)
36                             {
37                                 same++;
38                                 proList.Add(p);
39                                 if (same > 1)
40                                 {
41                                     for (int i = 0; i < proList.Count; i++)
42                                     {
43                                         if (((Process)(proList[i])).Id == currentProcess.Id)
44                                         {
45                                             Console.WriteLine("该进程已经启动了一个实例");
46                                             Thread.Sleep(1000);
47                                             ((Process)(proList[i])).Kill();
48                                         }
49                                     }
50                                 }
51                             }
52                         }
53                 }
54                 catch
55                 { }
56             }
57             Console.Read();
58         }
59     }
60 }
61 

 


方法四:直接定义一个属性类,利用此属性信息进行判断。
代码如下:
帮助属性  1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Reflection;
 6 using System.Diagnostics;
 7 using System.Collections;
 8 using System.Threading;
 9 
10 [assembly: Help("This Assembly demonstrates custom attributes creation and their run-time query.")]
11 
12 public class HelpAttribute : Attribute
13 {
14     public HelpAttribute(String Description_in)
15     {
16         this.description = Description_in;
17     }
18 
19     protected String description;
20 
21     public String Description
22     {
23         get
24         {
25             return this.description;
26         }
27     }
28 }
29 class Program
30 {
31     static void Main(string[] args)
32     {
33         HelpAttribute HelpAttr1 = null;
34         HelpAttribute HelpAttr2 = null;
35         Process currentProcess = Process.GetCurrentProcess(); //获取当前正在运行进程 
36         Assembly a = Assembly.LoadFrom(currentProcess.MainModule.FileName);
37         foreach (Attribute attr in a.GetCustomAttributes(true))
38         {
39             HelpAttr1 = attr as HelpAttribute;
40             if (null != HelpAttr1)
41             {
42                 //Console.WriteLine("Description of {0}:\n{1}", currentProcess.MainModule.FileName, HelpAttr1.Description); 
43                 break;
44             }
45         }
46         Process[] processes = Process.GetProcesses(); //获得当前所有进程 
47         int same = 0; //相同运行实例个数 
48         ArrayList proList = new ArrayList(); //将相同实例加入此集合中 
49         foreach (Process pro in processes)
50         {
51             try//由于进程不同,有的进程不包含Modules信息,所以要用try保护 
52             {
53                 if (pro.Modules != null)
54                     if (pro.Modules.Count > 0)
55                     {
56                         Assembly b = Assembly.LoadFrom(pro.MainModule.FileName);
57                         foreach (Attribute attr in b.GetCustomAttributes(true))
58                         {
59                             HelpAttr2 = attr as HelpAttribute;
60                             if (null != HelpAttr2)
61                             {
62                                 if (HelpAttr1.Description.Equals(HelpAttr2.Description))
63                                 {
64                                     same++;
65                                     proList.Add(pro);
66                                     if (same > 1)
67                                     {
68                                         for (int i = 0; i < proList.Count; i++)
69                                         {
70                                             if (((Process)(proList[i])).Id == currentProcess.Id)
71                                             {
72                                                 Console.WriteLine("该进程已经启动了一个实例");
73 
74                                                 Thread.Sleep(1000);
75                                                 ((Process)(proList[i])).Kill();
76 
77                                             }
78                                         }
79                                     }
80                                 }
81                             }
82                         }
83                     }
84             }
85             catch
86             {
87             }
88         }
89         Console.ReadLine();
90     }
91 
92 }

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载