C#event EventHandler事件触发

 

在定义委托时,前面加上event关键字,可以保证该委托不能在外部被随意触发,两者异同:

 注册注销内部触发外部触发
delegate+=-=InvokeInvoke
event delegate+=-=Invoke不允许

所以,event关键字有助于提高类的封装性,物理隔绝代码耦合,迫使类设计更追求高内聚。

定义一个显示消息的event并包装

        public event EventHandler evt_log_handle;
        protected virtual void On_evt_log_handle(object obj, EventArgs e)
        {
            if (this.evt_log_handle != null)
                this.evt_log_handle(obj, e);
        }

在外部触发:

On_evt_log_handle("日志", null);

在其他类中,可以注册这个事件

m_project.evt_log_handle += m_process_evt_log_handle;  m_project为定义event的类的实例

定义事件实现的代码

        void m_process_evt_log_handle(object sender, EventArgs e)
        {
            string sLog = sender as string;
            ...
        }

 控制台输入'a'累加到上限实现事件的示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Counter c = new Counter(new Random().Next(10));
            c.ThresholdReached += c_ThresholdReached;  //注册事件

            Console.WriteLine("press 'a' key to increase total");
            while (Console.ReadKey(true).KeyChar == 'a')
            {
                Console.WriteLine("adding one");
                c.Add(1);
            }
        }

        static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e) //事件实现
        {
            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
            Environment.Exit(0);
        }
    }

    class Counter
    {
        private int threshold;
        private int total;

        public Counter(int passedThreshold)
        {
            threshold = passedThreshold;
        }

        public void Add(int x)
        {
            total += x;
            if (total >= threshold)
            {
                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
                args.Threshold = threshold;
                args.TimeReached = DateTime.Now;
                OnThresholdReached(args);
            }
        }

        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached; 
        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            if (this.ThresholdReached != null)
            {
                this.ThresholdReached(null, e);
            }
        }
    }

    public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }

}

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值