通过监听本地设定的TCP端口实现,无网卡也没问题。解决防火墙问题,不会弹出防火墙询问。
1/*------------------------------------------
2* 模块名称:确保单进程类
3* 设计作者:张鹏
4* 设计日期:2009-6-4
5* 备注:大风扇吹空香皂盒
6*------------------------------------------*/
7using System;
8using System.Collections.Generic;
9using System.Text;
10using System.Net.Sockets;
11using System.IO;
12using System.Threading;
13using System.Net;
14
15namespace aaaSoft
16{
17 /// <summary>
18 /// 单进程类
19 /// </summary>
20 class SingleProcess
21 {
22 private string GuidString;
23 private int TcpPort;
24 private TcpListener listener;
25
26 /// <summary>
27 /// 构造函数
28 /// </summary>
29 /// <param name="GuidString">唯一标识符</param>
30 /// <param name="TcpPort">监听TCP端口号</param>
31 public SingleProcess(string GuidString, int TcpPort)
32 {
33 this.GuidString = GuidString;
34 this.TcpPort = TcpPort;
35 }
36
37 /// <summary>
38 /// 当第二进程运行时引发
39 /// </summary>
40 public event EventHandler SecondProcessStart;
41
42 /// <summary>
43 /// 开始确保单进程
44 /// </summary>
45 public void Start()
46 {
47 try
48 {
49 listener = new TcpListener(IPAddress.Parse("127.0.0.1"), TcpPort);
50 listener.Start();
51 Thread trdListen = new Thread(Run);
52 trdListen.Start();
53 }
54 catch
55 {
56 //说明已经有实例存在
57 TcpClient client = new TcpClient();
58 client.Connect("127.0.0.1", TcpPort);
59 NetworkStream ns = client.GetStream();
60 byte[] buffer = Encoding.Default.GetBytes(GuidString);
61 ns.Write(buffer, 0, buffer.Length);
62 ns.Close();
63 Environment.Exit(0);
64 }
65 }
66 /// <summary>
67 /// 结束确保单进程
68 /// </summary>
69 public void Stop()
70 {
71 listener.Stop();
72 }
73
74 private void Run()
75 {
76 try
77 {
78 while (true)
79 {
80 TcpClient client = listener.AcceptTcpClient();
81 NetworkStream ns = client.GetStream();
82 StreamReader sr = new StreamReader(ns);
83 string s = sr.ReadToEnd();
84 ns.Close();
85 if (s.Equals(GuidString))
86 {
87 //触发事件
88 SecondProcessStart.Invoke(null, null);
89 }
90 }
91 }
92 catch { }
93 }
94 }
95}
2* 模块名称:确保单进程类
3* 设计作者:张鹏
4* 设计日期:2009-6-4
5* 备注:大风扇吹空香皂盒
6*------------------------------------------*/
7using System;
8using System.Collections.Generic;
9using System.Text;
10using System.Net.Sockets;
11using System.IO;
12using System.Threading;
13using System.Net;
14
15namespace aaaSoft
16{
17 /// <summary>
18 /// 单进程类
19 /// </summary>
20 class SingleProcess
21 {
22 private string GuidString;
23 private int TcpPort;
24 private TcpListener listener;
25
26 /// <summary>
27 /// 构造函数
28 /// </summary>
29 /// <param name="GuidString">唯一标识符</param>
30 /// <param name="TcpPort">监听TCP端口号</param>
31 public SingleProcess(string GuidString, int TcpPort)
32 {
33 this.GuidString = GuidString;
34 this.TcpPort = TcpPort;
35 }
36
37 /// <summary>
38 /// 当第二进程运行时引发
39 /// </summary>
40 public event EventHandler SecondProcessStart;
41
42 /// <summary>
43 /// 开始确保单进程
44 /// </summary>
45 public void Start()
46 {
47 try
48 {
49 listener = new TcpListener(IPAddress.Parse("127.0.0.1"), TcpPort);
50 listener.Start();
51 Thread trdListen = new Thread(Run);
52 trdListen.Start();
53 }
54 catch
55 {
56 //说明已经有实例存在
57 TcpClient client = new TcpClient();
58 client.Connect("127.0.0.1", TcpPort);
59 NetworkStream ns = client.GetStream();
60 byte[] buffer = Encoding.Default.GetBytes(GuidString);
61 ns.Write(buffer, 0, buffer.Length);
62 ns.Close();
63 Environment.Exit(0);
64 }
65 }
66 /// <summary>
67 /// 结束确保单进程
68 /// </summary>
69 public void Stop()
70 {
71 listener.Stop();
72 }
73
74 private void Run()
75 {
76 try
77 {
78 while (true)
79 {
80 TcpClient client = listener.AcceptTcpClient();
81 NetworkStream ns = client.GetStream();
82 StreamReader sr = new StreamReader(ns);
83 string s = sr.ReadToEnd();
84 ns.Close();
85 if (s.Equals(GuidString))
86 {
87 //触发事件
88 SecondProcessStart.Invoke(null, null);
89 }
90 }
91 }
92 catch { }
93 }
94 }
95}
==================================================================
下面是使用示例,仅需要修改Program.cs文件(Demo项目Program.cs文件如下):
==================================================================
1using System;
2using System.Collections.Generic;
3using System.Windows.Forms;
4
5namespace 单实例运行
6{
7 static class Program
8 {
9 const int TcpPort = 53256;
10 const string GuidString = "C0DA3908-7F39-45ad-8E5B-CC47064031EA";
11 public static aaaSoft.SingleProcess sp;
12
13 public static Form frmMain;
14 /// <summary>
15 /// 应用程序的主入口点。
16 /// </summary>
17 [STAThread]
18 static void Main()
19 {
20 Application.EnableVisualStyles();
21 Application.SetCompatibleTextRenderingDefault(false);
22 frmMain = new Form1();
23
24 sp = new aaaSoft.SingleProcess(GuidString, TcpPort);
25 sp.SecondProcessStart += new EventHandler(sp_SecondProcessStart);
26 sp.Start();
27 Application.Run(frmMain);
28 sp.Stop();
29 }
30
31 static void sp_SecondProcessStart(object sender, EventArgs e)
32 {
33 frmMain.WindowState = FormWindowState.Normal;
34 frmMain.Show();
35 frmMain.Activate();
36 }
37 }
38}
2using System.Collections.Generic;
3using System.Windows.Forms;
4
5namespace 单实例运行
6{
7 static class Program
8 {
9 const int TcpPort = 53256;
10 const string GuidString = "C0DA3908-7F39-45ad-8E5B-CC47064031EA";
11 public static aaaSoft.SingleProcess sp;
12
13 public static Form frmMain;
14 /// <summary>
15 /// 应用程序的主入口点。
16 /// </summary>
17 [STAThread]
18 static void Main()
19 {
20 Application.EnableVisualStyles();
21 Application.SetCompatibleTextRenderingDefault(false);
22 frmMain = new Form1();
23
24 sp = new aaaSoft.SingleProcess(GuidString, TcpPort);
25 sp.SecondProcessStart += new EventHandler(sp_SecondProcessStart);
26 sp.Start();
27 Application.Run(frmMain);
28 sp.Stop();
29 }
30
31 static void sp_SecondProcessStart(object sender, EventArgs e)
32 {
33 frmMain.WindowState = FormWindowState.Normal;
34 frmMain.Show();
35 frmMain.Activate();
36 }
37 }
38}