方法一:遍历正在有相同名字运行的例程
方法二:通过同步进程实例,看其是否已经创建
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 }
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 }
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 }