庖丁解牛看委托和事件(续)
生活随笔
收集整理的這篇文章主要介紹了
庖丁解牛看委托和事件(续)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇文章:庖丁解牛——深入解析委托和事件之后,以一題面試題來總結事件
?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;namespace CallChange {public class Call{static void Main(string[] args){MessageMonitoring mm = new MessageMonitoring();//添加監聽事件的響應函數mm.OnValueChange += new MMEventHandle((s, e) =>{Console.WriteLine(e.Value);});mm.Run();}}//參數epublic class MMEventArgs : EventArgs{public int Value { get; set; }}public delegate void MMEventHandle(object sender, MMEventArgs e);//信息監控的類,當Value改變的時候,出發事件OnValueChangepublic class MessageMonitoring{public event MMEventHandle OnValueChange;private int _value;public void Run(){Thread thread = new Thread(() =>{while (true){Thread.Sleep(1000);++Value;}});thread.Start();}//屬性改變public int Value{get { return _value; }set{if (_value != value){_value = value;if (OnValueChange != null){OnValueChange(this, new MMEventArgs { Value = _value });}}}}} }轉載于:https://www.cnblogs.com/OceanEyes/archive/2012/08/07/eventmianshi.html
總結
以上是生活随笔為你收集整理的庖丁解牛看委托和事件(续)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 七、模型文档
- 下一篇: HDOJ---1272 小希的迷宫