C#笔记⑥——事件

事件

事件模型由五大部分组成:
1、事件拥有者(Event Source 对象)object sender
2、事件成员(Event 成员)
3、事件的响应者(Event Subcriber 对象)
4、事件处理器(Event Handler 成员)本质是个回调函数
5、事件订阅——把事件处理器与事件关联在一起,本质是以委托为基础的成员

image

订阅在同一方法体里

一个事件被多个事件处理器订阅
    class Test
    {
        static void Main(string[] args)
        {
            System.Timers.Timer timer=new System.Timers.Timer();
            timer.Interval=1000;
            Tom tom=new Tom();
            Jim jim=new Jim();
            timer.Elapsed +=tom.Action; //此为订阅
            timer.Elapsed +=jim.Action;
            timer.Start();
            Console.ReadLine();
        }

    }

    class Tom
    {
        internal void Action(object sender,ElapsedEventArgs e)
        {
            System.Console.WriteLine("I'm Tom");
        }
    }

    class Jim
    {
        internal void Action(object sender,ElapsedEventArgs e)
        {
            System.Console.WriteLine("I'm Jim");
        }
    }


不同class之间订阅

点击查看代码
    class Test
    {
        static void Main(string[] args)
        {
            System.Timers.Timer timer=new System.Timers.Timer(); //事件的拥有者
            ControlT control=new ControlT(timer); //事件的响应者
            System.Console.ReadLine();
        }

    }

    class ControlT
    {
        private System.Timers.Timer timer;

        public ControlT(System.Timers.Timer timer)
        {
            this.timer=timer;
            this.timer.Interval=1000;
            this.timer.Elapsed+=this.Action;
            this.timer.Start();
        }

        public void Action(object sender,ElapsedEventArgs e)
        {
            Console.ForegroundColor=ConsoleColor.Red;
            System.Console.WriteLine(DateTime.UtcNow.ToString());
        }

    }

对自身类型的订阅(继承或者叫派生)

点击查看代码
    class Test
    {
        static void Main(string[] args)
        {
            ControlT timer=new ControlT();
            timer.Interval=1000;
            timer.Elapsed+=timer.Action;
            timer.Start();
            System.Console.ReadLine();
        }

    }

    class ControlT:System.Timers.Timer
    {

        public void Action(object sender,ElapsedEventArgs e)
        {
            Console.ForegroundColor=ConsoleColor.Red;
            System.Console.WriteLine(DateTime.UtcNow.ToString());
        }

    }

事件拥有者是这个类的一个字段

点击查看代码
    class Test
    {
        static void Main(string[] args)
        {
            ControlT timer=new ControlT();
            Console.ReadLine();
        }

    }

    class ControlT:System.Timers.Timer
    {
        private System.Timers.Timer timer; //自身类的字段,事件拥有者

        public ControlT()
        {
            this.timer = new System.Timers.Timer();
            this.timer.Interval=1000;
            this.timer.Elapsed+=this.Action;   //被自身所订阅
            this.timer.Start();
        }

        internal void Action(object sensder,ElapsedEventArgs e)
        {
            Console.ForegroundColor=ConsoleColor.Gray;
            System.Console.WriteLine(DateTime.UtcNow.ToString());
        }
    }

同一触发器订阅不同事件

点击查看代码
    class Test
    {
        static void Main(string[] args)
        {
            ControlT timer=new ControlT();
            Console.ReadLine();
        }

    }

    class ControlT:System.Timers.Timer
    {
        private System.Timers.Timer timer1; //自身类的字段,事件拥有者
        private System.Timers.Timer timer2;

        public ControlT()
        {
            this.timer1 = new System.Timers.Timer();
            this.timer1.Interval=1000;
            this.timer1.Elapsed+=this.Action;   
            this.timer1.Start();

            this.timer2 = new System.Timers.Timer();
            this.timer2.Interval=3000;
            this.timer2.Elapsed+=this.Action;   
            this.timer2.Start();
        }

        internal void Action(object sensder,ElapsedEventArgs e)
        {
            if(sensder==timer1)
            {
            Console.ForegroundColor=ConsoleColor.Gray;
            System.Console.WriteLine(DateTime.UtcNow.ToString());
            }

            if(sensder==timer2)
            {
            Console.ForegroundColor=ConsoleColor.Red;
            System.Console.WriteLine("I am two");
            }

        }
    }

以下是老式事件订阅方式

this.button.Click +=new EventHandler(this.Action);

匿名写法

delegate写法
            this.timer2 = new System.Timers.Timer();
            this.timer2.Interval=3000;
            this.timer2.Elapsed+=delegate(object sensder,ElapsedEventArgs e) 
                                {
                                Console.ForegroundColor=ConsoleColor.Red;
                                System.Console.WriteLine("I am three");
                                };
            this.timer2.Start();
拉姆达表达式
            this.timer2 = new System.Timers.Timer();
            this.timer2.Interval=2000;
            this.timer2.Elapsed+=(sender,e)=>{
                                Console.ForegroundColor=ConsoleColor.Red;
                                System.Console.WriteLine("I am three");
                                };

            this.timer2.Start();
posted @ 2022-02-05 20:31  Ariaaaaa  阅读(7)  评论(0)    收藏  举报