C#自定义事件监视变量变化

首先监视定义类

 class Event
     {
         public delegate void tempChange(object sender, EventArgs e);
         public event tempChange OntempChange;
         string temp;
         public string Temp
         {
             get
             {
                 return temp;
             }
             set
             {
                 if (temp != value)
                 {
                     OntempChange(this, new EventArgs());
                 }
                 temp = value;
             }
         }
     }

使用事件:

        static Event ven;
        static void Main(string[] args)
        {
            ven = new Event();
            ven.OntempChange += new Event.tempChange(ven_OntempChange);
            for (int i = 0; i < 10; i++)
            {
                ven.Temp = i +"aaa";
                Thread.Sleep(1000);
            }
        }
        static void ven_OntempChange(object sender, EventArgs e)
        {
            Console.WriteLine(ven.Temp +"\r");
        }

 输出结果:

C#自定义事件监视变量变化_第1张图片

 

转载于:https://www.cnblogs.com/weifeng123/p/10233346.html

你可能感兴趣的:(C#自定义事件监视变量变化)