none
一个事件委托的问题? RRS feed

  • 常规讨论

  • 各位

    //class 1
    namespace Link
    {
        public class AsynSocketListener
        {
            private static AsynSocketListener _asynl = null;
            public static AsynSocketListener Instance
            {
                get
                {
                    if (_asynl == null)
                    {
                        _asynl = new AsynSocketListener();
                    }
                    return _asynl;
                }
            }
    
            /// <summary>
            /// 构造
            /// </summary>
            public AsynSocketListener()
            {
                //check alive
                keepalivetimer = new Timer(this.CheckAlive, null, 500, 500);
    
            }
            /// <summary>
            /// 监控客户端udp信息
            /// </summary>
            /// <returns></returns>
            public void UDP_Monitor()
            {
                sock = new Socket(AddressFamily.InterNetwork,
                SocketType.Dgram, ProtocolType.Udp);
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
                sock.Bind(iep);
                ep = (EndPoint)iep;
                sock.BeginReceiveFrom(returnmsgs, 0, returnmsgs.Length, SocketFlags.None, ref ep, new AsyncCallback(BeginRecieved), sock);
            }
    
     private void BeginRecieved(IAsyncResult ar)
            {
                Socket client = (Socket)ar.AsyncState;
                ResultEventArgs e = new ResultEventArgs();
                try
                {
                    //receive byte[]
                    int n = client.EndReceive(ar);
                    returnmsgstomsgs(n);
                    if (nb > 0)
                    {
                        e.Socket = client;
                        e.ReceiveByte = msgs;
                        e.RecievedCommandDateTime = DateTime.Now;
                        string userId = Encoding.ASCII.GetString(e.ReceiveByte, 3, 6);
                        if (msgs[0].ToString() == "57" && msgs[1].ToString() == "51" && msgs[nb - 1].ToString() == "52")
                        {
                            switch (msgs[2])
                            {
                                case 49:
                                    KeepAliveCheck kp = new KeepAliveCheck(userId.ToString());
                                    this.OnTerminalResultCompleted(this, e);
                                    break;
                           }
                            msgs = null;
                            nb = 0;
                        }
                    }
                    //继续非同步呼叫Receive
                    client.BeginReceive(returnmsgs, 0, returnmsgs.Length, SocketFlags.None, new AsyncCallback(BeginRecieved), client);
                }
                catch
                {
                    // throw;
                    msgs = null;
                    nb = 0;
    
                    client.BeginReceive(returnmsgs, 0, returnmsgs.Length, SocketFlags.None, new AsyncCallback(BeginRecieved), client);
                }
            }
    
            /// <summary>
            /// 机器回应事件
            /// </summary>
            public event EventHandler TerminalResulted;
    
            #region 机器回应触发事件
            private void OnTerminalResultCompleted(object sender, ResultEventArgs e)
            {
                if (this.TerminalResulted != null)
                {
                    this.TerminalResulted(this, e);
                }
            }
            #endregion
    
        }
    }
    
    
    
    
    //class 2
    
    namespace Link.Command
    {
        public class KeepAliveCheck : BaseSemacCommand
        {
            public KeepAliveCheck(string userId)
            {
                this.userId = userId;
                this.Command = 49;
            }
    
            /// <summary>
            /// 机器回应后触发的事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected override void Instance_TerminalResulted(object sender, EventArgs e)
            {
                ResultEventArgs re = (ResultEventArgs)e;
                string userId = Encoding.ASCII.GetString(re.ReceiveByte, 3, 6);
                if (AsynSocketListener.Instance.SocketIsIn(userId.ToString()))
                {
                    AsynSocketListener.Instance.SocketUpdateKeepAliveTime(re.Socket, re.ReceiveByte);
                }
                else
                {
                    SocketObject so = new SocketObject(re.Socket, userId.ToString(), re.ReceiveByte);
                    AsynSocketListener.Instance.SocketList.Add(so);
                }
            }
    
            public override byte[] CreateCommand()
            {
                byte[] r = new byte[11];
                return r;
            }
            public override byte[] CreateCommand(string[] a)
            {
                byte[] r = new byte[11];
                return r;
            }
        }
    }
    
    
    
    //class 3
    
    namespace Link.Command
    {
        /// <summary>
        /// 基类
        /// </summary>
        public abstract class BaseSemacCommand
        {
    
            #region 属性
            /// <summary>
            /// 指令送出的时间
            /// </summary>
            public DateTime SendCommandDateTime { set; get; }
            public string Key { get; set; }
            /// <summary>
            /// 16進位 Command
            /// </summary>
            protected byte Command = 0x00;
    
            /// <summary>
            /// 用户编号
            /// </summary>
            public string userId { set; get; }
    
            #endregion
    
            public abstract byte[] CreateCommand();
            public abstract byte[] CreateCommand(string[] data);
    
    
            public BaseSemacCommand()
            {
                this.SendCommandDateTime = DateTime.Now;
                this.Key = Guid.NewGuid().ToString();
    
                //声明事件
                AsynSocketListener.Instance.TerminalResulted += new EventHandler(Instance_TerminalResulted);
             }
    
            protected virtual void Instance_TerminalResulted(object sender, EventArgs e) { }
    
    
        }
    }
    

    先看代码

    在class 1 AsynSocketListener中是udp接受数据,在接收到数据有实例class 2 KeepAliveCheck 类 KeepAliveCheck 是继承与class3 BaseSemacCommand类,问题是在BaseSemacCommand中 AsynSocketListener.Instance.TerminalResulted += new EventHandler(Instance_TerminalResulted)添加了事件委托方法,现在问题是在KeepAliveCheck 类中运行到   private void OnTerminalResultCompleted(object sender, ResultEventArgs e)方法时 if (this.TerminalResulted != null)总是=null。有谁知道愿意的。希望大家帮帮我。小弟先谢了。


    pinkbull

    2012年3月8日 14:18

全部回复