事件和委托示例

来源:互联网 发布:大数据呼叫中心 编辑:程序博客网 时间:2024/05/16 09:37

1、Timer的Elapsed事件

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Timers;namespace Timer的Elapsed事件{    class Program    {        static void timer_Elased(Object sender,ElapsedEventArgs e)        {            Console.Clear();            Console.WriteLine();            Console.Write(" {0}", e.SignalTime.ToLongTimeString());        }        static void Main(string[] args)        {            Console.Title = "Timer的Elapsed事件应用示例";            Console.BackgroundColor = ConsoleColor.White;            Console.ForegroundColor = ConsoleColor.DarkBlue;            Console.Clear();            Timer timer = new Timer(1000);            timer.Elapsed += new ElapsedEventHandler(timer_Elased);            timer.Start();            Console.ReadKey();        }    }}

2、事件发行者

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 事件发行者{    public class Publisher    {        private int level;        public Publisher(int x)        {            level = x;        }        public event EventHandler Event1;        private event EventHandler event2;        public event EventHandler Event2        {            add            {                lock (this)                {                    if (level > 10)                    {                        event2 += new EventHandler(value);                    }                    else                        Console.WriteLine("提供的level值太低,未能引发事件Event2!");                }            }            remove            {                lock (this)                {                    event2 -= new EventHandler(value);                    Console.WriteLine("事件处理程序已从Event2事件的调用列表中移除!");                }            }        }        protected virtual void OnEvent1(EventArgs e)        {            if (Event1 != null) Event1(this, e);        }        protected virtual void OnEvent2(EventArgs e)        {            if (event2 != null) event2(this, e);        }        public void start()        {            EventArgs args = new EventArgs();            this.OnEvent1(args);            this.OnEvent2(args);        }    }    class Program    {        public static void pub_Event1(object sender,EventArgs e)        {            Console.WriteLine("引发事件 Event1……");        }        public static void pub_Event2(object sender,EventArgs e)        {            Console.WriteLine("引发事件Event2……");        }        static void Main(string[] args)        {            Console.Title = "事件发行者示例";            Console.BackgroundColor = ConsoleColor.White;            Console.ForegroundColor = ConsoleColor.DarkBlue;            Console.Clear();            Publisher pub1 = new Publisher(19);            pub1.Event1 += new EventHandler(pub_Event1);            pub1.Event2 += new EventHandler(pub_Event2);            pub1.start();            Console.WriteLine();            Publisher pub2 = new Publisher(3);            pub2.Event1 += new EventHandler(pub_Event1);            pub2.Event2 += new EventHandler(pub_Event2);            pub2.start();            Console.WriteLine();            pub2.Event1 -= new EventHandler(pub_Event1);            pub2.Event2 -= new EventHandler(pub_Event2);            pub2.start();            Console.WriteLine();        }    }}

3、特定事件处理

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 特定事件处理{    public class FireEventArgs:EventArgs    {        public string room;        public int ferocity;        public FireEventArgs(string room,int ferocity)        {            this.room = room;            this.ferocity = ferocity;        }    }    public class FireAlarm    {        public delegate void FireEventHandler(Object sender, FireEventArgs e);        public event FireEventHandler FireEvent;        public void OnFire(string room,int ferocity)        {            if(FireEvent!=null)            {                FireEventArgs e = new FireEventArgs(room, ferocity);                FireEvent(this, e);            }        }    }    class FireHandlerClass    {        public FireHandlerClass(FireAlarm fireAlarm)        {            fireAlarm.FireEvent += ExtinguishFire;        }        void ExtinguishFire(Object sender,FireEventArgs e)        {            Console.WriteLine("\n发生FireEvent事件:");            Console.WriteLine(" 由{0}调用ExtinguishFire方法", sender.ToString());            if (e.ferocity < 2)            {                Console.WriteLine(" {0}里的火无大碍,我用水把它浇灭", e.room);            }            else if (e.ferocity < 5)                Console.WriteLine("我用灭火器把{0}里的火扑灭", e.room);            else                Console.WriteLine(" {0}里的火失去控制了,我给消防队打电话", e.room);        }    }    class Program    {        static void Main(string[] args)        {            Console.Title = "特定事件处理示例";            Console.BackgroundColor = ConsoleColor.White;            Console.ForegroundColor = ConsoleColor.DarkBlue;            Console.Clear();            FireAlarm myFireAlarm = new FireAlarm();            FireHandlerClass myFireHandlerClass = new FireHandlerClass(myFireAlarm);            myFireAlarm.OnFire("厨房",3);            myFireAlarm.OnFire("书房", 1);            myFireAlarm.OnFire("走廊", 5);        }    }}

4、自定义事件类

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 自定义事件类{    class Program    {        static void Main(string[] args)        {            Console.Title = "自定义事件类";            Console.BackgroundColor = ConsoleColor.White;            Console.ForegroundColor = ConsoleColor.Red;            Console.Clear();            ConsoleKeyInfo cki;            //当Console.TreatControlCAsInput属性为false时,Ctrl+C被处理为一个中断,而不是输入            Console.TreatControlCAsInput = false;            Console.CancelKeyPress += new ConsoleCancelEventHandler(MyHandler);            while(true)            {                Console.WriteLine("请按任意键或按ESC键退出、或按Ctrl+C组合键中断读操作");                cki = Console.ReadKey(true);                Console.WriteLine("您按下了{0}键\n", cki.Key);                if(cki.Key==ConsoleKey.Escape) break;            }        }        protected static void MyHandler(Object sender,ConsoleCancelEventArgs e)        {            Console.WriteLine("\n事件处理程序执行结果如下:");            Console.WriteLine("控制台读操作已被中断");            Console.WriteLine(" 您按下了{0}键", e.SpecialKey);            Console.WriteLine("Cancel属性值为{0}", e.Cancel);            Console.WriteLine("设置Cancle属性为true……");            e.Cancel = true;            Console.WriteLine(" Cancel属性值已被设置为{0}", e.Cancel);            Console.WriteLine("控制台读操作现已恢复……");        }    }}

5、自定义事件示例

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 自定义事件示例{    public class FireAlarm    {        public event EventHandler FireEvent;        public void OnFireEvent()        {            EventArgs e = new EventArgs();            if(FireEvent!=null)            {                FireEvent(this, e);            }        }    }    public class FireHanldlerClass    {        /*public FireHanldlerClass(FireAlarm fireAlarm)        {            fireAlarm.FireEvent += new EventHandler(ExtinguishFire);        }*/        public void ExtinguishFire(Object sender,EventArgs e)        {            Console.WriteLine("\n 发生FireEvent事件:");            Console.WriteLine(" ExtinguishFire方法由{0}调用……", sender.ToString());            Console.WriteLine(" 着火了,赶快来救火啦………………\n");        }    }    class Program    {        static void Main(string[] args)        {            Console.Title = "自定义事件处理程序示例";            Console.BackgroundColor = ConsoleColor.White;            Console.ForegroundColor = ConsoleColor.DarkBlue;            Console.Clear();            FireAlarm myFireAlarm = new FireAlarm();            FireHanldlerClass myFireHandler = new FireHanldlerClass();            myFireAlarm.FireEvent += myFireHandler.ExtinguishFire;            myFireAlarm.OnFireEvent();        }    }}