触发事件,检查是否注册过事件的方法

下面的代码,会发现事件没有被注册

 class Program
    {
        static void Main(string[] args)
        {
            EventCheck check = new EventCheck();
            check.OnDataArrived(new DataArrivedEventArgs() { data="Hello World!"});
            Console.ReadKey();
        }

        static void check_DataArrived(object sender, DataArrivedEventArgs e)
        {
            Console.WriteLine(e.data);
        }
    }

    class EventCheck
    {
        //第三步 use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.
        //To associate the event with the method that will handle the event, add an instance of the delegate to the event. 
        //The event handler is called whenever the event occurs, unless you remove the delegate.
        internal event DataArrivedEventHandler DataArrived;
        
        //第四步
        internal void OnDataArrived(DataArrivedEventArgs e)
        {
            try
            {
                //DataArrivedEventHandler handler = DataArrived;
                //if (handler != null)
                //{
                //    handler(this, e);
                //}
                DataArrived += EventCheck_DataArrived;
                if (DataArrived != null)
                {
                    DataArrived -= EventCheck_DataArrived;
                    DataArrived(this, e);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("遇到异常{0}", ex.Message);
            }
        }

        void EventCheck_DataArrived(object sender, DataArrivedEventArgs e)
        {
            Console.WriteLine(e.data);
        }
    }

    //第二步   Represents the method that will handle an event when the event provides data.
    //This method's first parameter is of type Object and refers to the instance that raises the event. 
    //Its second parameter is derived from type EventArgs and holds the event data.
    //If the event does not generate event data, the second parameter is simply the value of the EventArgs.Empty field. 
    //Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.
    delegate void DataArrivedEventHandler(object sender,DataArrivedEventArgs e);  //正式写代码的时候,可以使用EventHandler<TEventArgs>泛型委托

    //第一步   
    //create a custom event data class, create a class that derives from the EventArgs class and provide the properties to store the necessary data. 
    //The name of your custom event data class should end with EventArgs.
    class DataArrivedEventArgs : EventArgs
    {
       internal string data;
    }

 

 

修改第四步的代码,先将事件复制到临时变量里面,然后通过这个临时变量来处理,就不会有问题

//第四步
        internal void OnDataArrived(DataArrivedEventArgs e)
        {
            try
            {
                //DataArrivedEventHandler handler = DataArrived;
                //if (handler != null)
                //{
                //    handler(this, e);
                //}
                DataArrived += EventCheck_DataArrived;
                DataArrivedEventHandler handler = DataArrived;
                if (handler != null)
                {
                    DataArrived -= EventCheck_DataArrived;
                    handler(this, e);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("遇到异常{0}", ex.Message);
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值