C#事件模型的一个实例

开发 后端
本文通过一个一个实例来介绍C#事件模型,供大家参考。

在.net设计开发过程中事件无疑是用的最多的,下面设计几个类来理解CLR事件工作机制:

C#事件模型应用场景:假设我设计一个送货上门的服务程序,当要送货时我将消息发送给送奶工或者邮递员等。

在构建应用程序时,我们先设计一个名为GoodsManager的类型负责发送消息,为GoodsManager类型定义一个对外提供名为NewGoods的事件。其它类型如"Milk"、“NewsPaper”可以订阅该事件。当GoodsManager接收到新的消息时,他将引发该事件,将消息分发给已订阅事件的对象。各个对象则以自己期望的方式来处理消息。

class Program     
    {     
        static void Main(string[] args)     
        {     
            GoodsManager gm = new GoodsManager();     
            Milk milk = new Milk(gm);     
            NewsPaper paper = new NewsPaper();     
            paper.Register(gm);     
            gm.SubmitGoods("Jacky","Lily","送货上门");     
    
            Console.WriteLine("**************");     
            //发生经济危机了取消Milk订阅     
            milk.UnRegister(gm);     
            gm.SubmitGoods("lily""Peter""送货上门");     
            Console.ReadLine();     
                 
        }     
    
        /// < summary>     
        /// 定义一个类型用于存放发送给事件接收者的附加信息     
        /// < /summary>     
        internal class NewGoodsEventArgs : EventArgs     
        {     
            private readonly String f_From, f_To, f_Desc;     
            public NewGoodsEventArgs(String from,String to,String desc)     
            {     
                f_From  = from;     
                f_To    = to;     
                f_Desc  = desc;     
            }     
            public String From { get { return f_From; } }     
            public String To { get { return f_To; } }     
            public String Desc { get { return f_Desc; } }     
        }     
             
        //定义事件成员管理类     
        internal class GoodsManager     
        {     
            //创建线程同步锁的私有字段     
            private readonly Object m_eventLock = new Object();     
    
            private EventHandler< NewGoodsEventArgs> m_NewGoods;     
            //增加一个事件成员     
            public event EventHandler< NewGoodsEventArgs> NewGoods     
            {     
                //加私有锁并向委托链表增加一个处理程序     
                add { lock (m_eventLock) { m_NewGoods += value; } }     
                //显示实现Remove方法     
                remove { lock (m_eventLock) { m_NewGoods -= value; } }     
            }     
                 
            //引发事件的方法,以通知订阅者对像事件已发生。     
            protected virtual void OnNewGoods(NewGoodsEventArgs e)     
            {     
                EventHandler< NewGoodsEventArgs> temp = m_NewGoods;     
                if (temp != null)     
                    temp(this,e);     
            }     
            //将输入转化为期望事件     
            public void SubmitGoods(String from, String to, String desc)     
            {     
                //构个对象用来通知接受者信息     
                NewGoodsEventArgs e = new NewGoodsEventArgs(from,to,desc);     
                //调用虚方法通知事件已发生     
                OnNewGoods(e);     
            }     
        }     
    
            
        //监听事件的类型     
        internal sealed class Milk     
        {     
            public Milk() { }     
            public Milk(GoodsManager gm)     
            {     
                gm.NewGoods += new EventHandler< NewGoodsEventArgs>(Milk_NewGoods);     
            }     
    
            //当新的物品送到时执行此方法     
            void Milk_NewGoods(object sender, NewGoodsEventArgs e)     
            {     
                Console.WriteLine("Accept Milk");     
                Console.WriteLine("From:{0},To:{1},desc:{2}",e.From,e.To,e.Desc);     
            }     
    
            public void UnRegister(GoodsManager gm)//注销事件     
            {     
                gm.NewGoods -= Milk_NewGoods;     
            }     
            public void Register(GoodsManager gm) //订阅事件     
            {     
                gm.NewGoods += Milk_NewGoods;     
            }     
        }     
    
        internal sealed class NewsPaper     
        {     
            public NewsPaper() { }     
            public NewsPaper(GoodsManager gm)     
            {     
                gm.NewGoods += new EventHandler< NewGoodsEventArgs>(Milk_NewGoods);     
            }     
            void Milk_NewGoods(object sender, NewGoodsEventArgs e)     
            {     
                Console.WriteLine("Accept News Paper");     
                Console.WriteLine("From:{0},To:{1},desc:{2}", e.From, e.To, e.Desc);     
            }     
            public void UnRegister(GoodsManager gm)     
            {     
                gm.NewGoods -= Milk_NewGoods;     
            }     
            public void Register(GoodsManager gm)     
            {     
                gm.NewGoods += Milk_NewGoods;     
            }     
        }     
    }   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.

C#事件模型执行结果:

执行结果 

C#事件模型的例子就介绍到这里。

【编辑推荐】

  1. C# winForm自定义鼠标样式的两种方法
  2. C#自定义消息框的设置图解
  3. 掌握C#自定义泛型类:从初始化说起
  4. C#存储过程的循序渐进
  5. 存储过程的优势及其调用方法介绍

 

责任编辑:book05 来源: csdn
相关推荐

2009-07-30 18:18:27

C#时间计算

2009-09-01 16:14:06

C#窗口抖动

2009-08-31 14:01:50

C#创建一个文件

2009-09-03 16:38:49

C#回车键事件

2009-09-09 12:55:59

C# TextBox事

2009-10-09 09:07:40

C#委托和事件

2024-05-14 08:03:51

C#EventArgs​属性

2009-07-22 17:15:04

C#实现

2011-09-16 10:00:56

C++

2009-08-19 14:15:42

C# 复合控件

2009-08-05 16:04:27

C# Actor模型

2011-07-18 10:01:59

C# ADO.NETSQL Server数

2011-07-18 10:45:55

C#SQL Server数

2009-08-26 15:53:42

C#数据访问XML

2009-08-25 01:46:00

C# WINDOWS服

2009-08-18 10:48:25

C#事件

2009-09-11 09:11:09

2009-08-31 14:19:20

C#打开一个文件

2009-09-01 16:03:32

C#单元测试

2009-08-31 13:53:03

C#创建一个文件
点赞
收藏

51CTO技术栈公众号