C# 實時監聽值的變化


代碼:

 1 using System;
 2 namespace Test_Tools
 3 {
 4 
 5     class Program 
 6     {
 7         static void Main(string[] args)
 8         {
 9 
10             MonitorValueChange change = new MonitorValueChange();
11             change.MyValue = 10;
12             change.OnMyValueChanged += Change_OnMyValueChanged;
13             change.MyValue = 10;
14 
15            
16         }
17         public static void Change_OnMyValueChanged(object sender, EventArgs e)
18         {
19             Console.WriteLine("&^%&%^&%^");
20             //要做的操作
21         }
22     }
23     public class MonitorValueChange
24     {
25         private int myValue;
26         public int MyValue
27         {
28             get { return myValue; }
29             set
30             {
31                 if (value != myValue)
32                 {
33                     WhenMyValueChange();
34                 }
35                 myValue = value;
36             }
37         }
38         //定義的委托
39         public delegate void MyValueChanged(object sender, EventArgs e);
40         //與委托相關聯的事件
41         public event MyValueChanged OnMyValueChanged;
42         //事件觸發函數
43         private void WhenMyValueChange()
44         {
45             if (OnMyValueChanged != null)
46             {
47                 OnMyValueChanged(this, null);
48 
49             }
50         }
51 
52     }
53 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM