C#实现确保单进程类

通过监听本地设定的TCP端口实现,无网卡也没问题。解决防火墙问题,不会弹出防火墙询问。

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*------------------------------------------
 2* 模块名称:确保单进程类
 3* 设计作者:张鹏
 4* 设计日期:2009-6-4
 5* 备注:大风扇吹空香皂盒
 6*------------------------------------------*/

 7 using  System;
 8 using  System.Collections.Generic;
 9 using  System.Text;
10 using  System.Net.Sockets;
11 using  System.IO;
12 using  System.Threading;
13 using  System.Net;
14
15 namespace  aaaSoft
16 ExpandedBlockStart.gifContractedBlock.gif {
17ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
18    /// 单进程类
19    /// </summary>

20    class SingleProcess
21ExpandedSubBlockStart.gifContractedSubBlock.gif    {
22        private string GuidString;
23        private int TcpPort;
24        private TcpListener listener;
25
26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
27        /// 构造函数
28        /// </summary>
29        /// <param name="GuidString">唯一标识符</param>
30        /// <param name="TcpPort">监听TCP端口号</param>

31        public SingleProcess(string GuidString, int TcpPort)
32ExpandedSubBlockStart.gifContractedSubBlock.gif        {
33            this.GuidString = GuidString;
34            this.TcpPort = TcpPort;
35        }

36
37ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
38        /// 当第二进程运行时引发
39        /// </summary>

40        public event EventHandler SecondProcessStart;
41
42ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
43        /// 开始确保单进程
44        /// </summary>

45        public void Start()
46ExpandedSubBlockStart.gifContractedSubBlock.gif        {
47            try
48ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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
55ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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        }

66ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
67        /// 结束确保单进程
68        /// </summary>

69        public void Stop()
70ExpandedSubBlockStart.gifContractedSubBlock.gif        {
71            listener.Stop();
72        }

73
74        private void Run()
75ExpandedSubBlockStart.gifContractedSubBlock.gif        {
76            try
77ExpandedSubBlockStart.gifContractedSubBlock.gif            {
78                while (true)
79ExpandedSubBlockStart.gifContractedSubBlock.gif                {
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))
86ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
87                        //触发事件
88                        SecondProcessStart.Invoke(nullnull);
89                    }

90                }

91            }

92ExpandedSubBlockStart.gifContractedSubBlock.gif            catch { }
93        }

94    }

95}

==================================================================

下面是使用示例,仅需要修改Program.cs文件(Demo项目Program.cs文件如下):

==================================================================


 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Windows.Forms;
 4
 5 namespace  单实例运行
 6 ExpandedBlockStart.gifContractedBlock.gif {
 7    static class Program
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 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;
14ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
15        /// 应用程序的主入口点。
16        /// </summary>

17        [STAThread]
18        static void Main()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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)
32ExpandedSubBlockStart.gifContractedSubBlock.gif        {
33            frmMain.WindowState = FormWindowState.Normal;
34            frmMain.Show();
35            frmMain.Activate();
36        }

37    }

38}

转载于:https://www.cnblogs.com/aaaSoft/archive/2009/07/28/1533301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值