当前位置: 移动技术网 > IT编程>开发语言>.net > 事件Event一

事件Event一

2019年04月18日  | 移动技术网IT编程  | 我要评论

大唐樊梨花全集,家产电视剧全集下载,采矿证

事件(event)例如:最近的视觉中国‘黑洞事件’。我们大多数人(订阅者)是通过xx平台(发布者)得知的这一消息,然后订阅者a出售视觉中国的股票(触发的方法),订阅者b买入视觉中国的股票。

  1 using system;
  2 using system.collections.generic;
  3 using system.linq;
  4 using system.text;
  5 using system.threading;
  6 
  7 namespace consoleevent
  8 {
  9     class program
 10     {
 11         static void main(string[] args)
 12         {
 13             //实例化一个事件对象
 14             media eventsource = new media("视觉中国:黑洞也是我们家的!");
 15 
 16             //实例化关注事件的对象(订阅者)
 17             stupiderone obj1 = new stupiderone();
 18             stupidertwo obj2 = new stupidertwo();
 19 
 20             //使用委托把对象及其方法注册到事件中
 21             eventsource.blackholeevent += new blackholeeventhandle(obj1.sendstock);
 22             eventsource.blackholeevent += new blackholeeventhandle(obj2.buystock);
 23 
 24             //事件到了触发黑洞事件,事件的调用
 25             eventsource.timeup();
 26             console.read();
 27         }
 28     }
 29 
 30     //第一步:定义一个类型用来保存所有需要发送给事件接收者的附加信息
 31     public class blackholeeventargs : eventargs
 32     { 
 33         //表示事件的内容
 34         private readonly string content;
 35 
 36         public string content
 37         {
 38             get { return content; }
 39         }
 40 
 41         public blackholeeventargs(string content)
 42         {
 43             this.content = content;
 44         }
 45     }
 46 
 47     //第二步:定义一个黑洞事件,首先需要定义一个委托类型,用于指定事件触发时调用的方法类型
 48     public delegate void blackholeeventhandle(object sender, blackholeeventargs e);
 49     //定义事件成员
 50     public class subject
 51     { 
 52         //定义黑洞事件
 53         public event blackholeeventhandle blackholeevent;
 54 
 55         //第三步:定义一个负责引发事件的方法,它通知已关注的对象
 56         protected virtual void notify(blackholeeventargs e)
 57         { 
 58            //出于线程安全的考虑,现在将对委托字段的引用复制到一个临时字段中
 59             blackholeeventhandle temp = interlocked.compareexchange(ref blackholeevent, null, null);
 60             if (temp != null)
 61             { 
 62                //触发事件,与方法的使用方式相同
 63                //事件通知委托对象,委托对象调用封装的方法
 64                 temp(this,e);
 65             }
 66         }
 67     }
 68 
 69     //定义触发事件的对象,事件源
 70     public class media : subject
 71     {
 72         private string content;
 73         public media(string content)
 74         {
 75             this.content = content;
 76         }
 77         public void timeup()
 78         {
 79             blackholeeventargs eventarg = new blackholeeventargs(content);
 80             //黑洞门来了,通知吃瓜们
 81             this.notify(eventarg);
 82         }
 83     }
 84 
 85     //吃瓜对象
 86     public class stupiderone
 87     {
 88         public void sendstock(object sender, blackholeeventargs e)
 89         {
 90             console.writeline(e.content + "呵呵,我要抓紧出售视觉中国的股票");
 91         }
 92     }
 93 
 94     //吃瓜对象
 95     public class stupidertwo
 96     {
 97         public void buystock(object sender, blackholeeventargs e)
 98         {
 99             console.writeline(e.content + "嘿嘿,我要等视觉中国的股票跌到底后,买入它的股票");
100         }
101     }
102 }

运行结果:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网