您的位置:首页 > 其它

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

2010-12-15 10:33 246 查看
方法一:
导入C++ API
1
2         /// <summary>
3         /// 从这里开始运行
4         /// </summary>
5         [STAThread]
6         static void Main()
7         {
8             Process instance = RunningInstance();
9             if (instance == null)
10             {
11                 //没有实例在运行
12                 WeatherApp appInstance = new WeatherApp();
13                 appInstance.StartMainGui();
14             }
15             else
16             {
17                 //已经有一个实例在运行
18                 HandleRunningInstance(instance);
19             }
20         }
21         #region 确保只有一个实例
22         public static Process RunningInstance()
23         {
24             Process current = Process.GetCurrentProcess();
25             Process[] processes = Process.GetProcessesByName(current.ProcessName);
26             //遍历与当前进程名称相同的进程列表
27             foreach (Process process in processes)
28             {
29                 //Ignore the current process
30                 if (process.Id != current.Id)
31                 {
32                     //Make sure that the process is running from the exe file.
33                     if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName)
34                     {
35                         //Return the other process instance.
36                         return process;
37                     }
38                 }
39             }
40             return null;
41         }
42         private static void HandleRunningInstance(Process instance)
43         {
44             MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
45             ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
46             SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
47         }
48         [DllImport("User32.dll")]
49         private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
50         [DllImport("User32.dll")]
51         private static extern bool SetForegroundWindow(System.IntPtr hWnd);
52         #endregion
53

方法二:
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 }


转载自:http://www.cnblogs.com/huang_8228/archive/2010/12/14/1905552.html 黄颖劲的博客
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐