提供以指定的時間間隔對線程池線程執行方法的機制android
1 using System; 2 using System.Threading; 3 4 class TimerExample 5 { 6 static void Main() 7 { 8 // Create an AutoResetEvent to signal the timeout threshold in the 9 // timer callback has been reached. 10 var autoEvent = new AutoResetEvent(false); 11 12 var statusChecker = new StatusChecker(10); 13 14 // Create a timer that invokes CheckStatus after one second, 15 // and every 1/4 second thereafter. 16 Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n", 17 DateTime.Now); 18 var stateTimer = new Timer(statusChecker.CheckStatus, 19 autoEvent, 1000, 250); 20 21 // When autoEvent signals, change the period to every half second. 22 autoEvent.WaitOne(); 23 stateTimer.Change(0, 500); 24 Console.WriteLine("\nChanging period to .5 seconds.\n"); 25 26 // When autoEvent signals the second time, dispose of the timer. 27 autoEvent.WaitOne(); 28 stateTimer.Dispose(); 29 Console.WriteLine("\nDestroying timer."); 30 } 31 } 32 33 class StatusChecker 34 { 35 private int invokeCount; 36 private int maxCount; 37 38 public StatusChecker(int count) 39 { 40 invokeCount = 0; 41 maxCount = count; 42 } 43 44 // This method is called by the timer delegate. 45 public void CheckStatus(Object stateInfo) 46 { 47 AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; 48 Console.WriteLine("{0} Checking status {1,2}.", 49 DateTime.Now.ToString("h:mm:ss.fff"), 50 (++invokeCount).ToString()); 51 52 if(invokeCount == maxCount) 53 { 54 // Reset the counter and signal the waiting thread. 55 invokeCount = 0; 56 autoEvent.Set(); 57 } 58 } 59 } 60 // The example displays output like the following: 61 // 11:59:54.202 Creating timer. 62 // 63 // 11:59:55.217 Checking status 1. 64 // 11:59:55.466 Checking status 2. 65 // 11:59:55.716 Checking status 3. 66 // 11:59:55.968 Checking status 4. 67 // 11:59:56.218 Checking status 5. 68 // 11:59:56.470 Checking status 6. 69 // 11:59:56.722 Checking status 7. 70 // 11:59:56.972 Checking status 8. 71 // 11:59:57.223 Checking status 9. 72 // 11:59:57.473 Checking status 10. 73 // 74 // Changing period to .5 seconds. 75 // 76 // 11:59:57.474 Checking status 1. 77 // 11:59:57.976 Checking status 2. 78 // 11:59:58.476 Checking status 3. 79 // 11:59:58.977 Checking status 4. 80 // 11:59:59.477 Checking status 5. 81 // 11:59:59.977 Checking status 6. 82 // 12:00:00.478 Checking status 7. 83 // 12:00:00.980 Checking status 8. 84 // 12:00:01.481 Checking status 9. 85 // 12:00:01.981 Checking status 10. 86 // 87 // Destroying timer.
1 using System; 2 using System.Threading; 3 4 class TimerExample 5 { 6 public static int loop_index = 1; 7 public static AutoResetEvent autoEvent = new AutoResetEvent(false); 8 9 static void Main() 10 { 11 int loop_count = 10; 12 13 // Create an AutoResetEvent to signal the timeout threshold in the 14 // timer callback has been reached. 15 16 17 // Create a timer that invokes CheckStatus after one second, 18 // and every 1/4 second thereafter. 19 Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n", 20 DateTime.Now); 21 var stateTimer = new Timer(show, 22 autoEvent, 1000, 1000); 23 24 // When autoEvent signals, change the period to every half second. 25 autoEvent.WaitOne(); 26 stateTimer.Change(0, 500); 27 Console.WriteLine("\nChanging period to .5 seconds.\n"); 28 29 // When autoEvent signals the second time, dispose of the timer. 30 autoEvent.WaitOne(); 31 stateTimer.Dispose(); 32 Console.WriteLine("\nDestroying timer."); 33 } 34 35 public static void show(Object stateInfo) 36 { 37 Console.WriteLine("{0:h:mm:ss.fff} Run in Show.\n", 38 DateTime.Now); 39 Console.WriteLine(loop_index); 40 loop_index = loop_index + 1; 41 if (loop_index > 10) 42 { 43 autoEvent.Set(); 44 } 45 } 46 }
Timer(TimerCallback) | |
Timer(TimerCallback, Object, Int32, Int32) | 使用 32 位的有符號整數指定時間間隔,初始化 |
Timer(TimerCallback, Object, Int64, Int64) | 用 64 位有符號整數來度量時間間隔,以初始化 |
Timer(TimerCallback, Object, TimeSpan, TimeSpan) | 初始化 |
Timer(TimerCallback, Object, UInt32, UInt32) | 用 32 位無符號整數來度量時間間隔,以初始化 |
Change(Int32, Int32) | 更改計時器的啓動時間和方法調用之間的間隔,用 32 位有符號整數度量時間間隔。spa |
Change(Int64, Int64) | 更改計時器的啓動時間和方法調用之間的間隔,用 64 位有符號整數度量時間間隔。線程 |
Change(TimeSpan, TimeSpan) | 更改計時器的啓動時間和方法調用之間的時間間隔,使用 TimeSpan 值度量時間間隔。代理 |
Change(UInt32, UInt32) | 更改計時器的啓動時間和方法調用之間的間隔,用 32 位無符號整數度量時間間隔。code |
CreateObjRef(Type) | 建立一個對象,該對象包含生成用於與遠程對象進行通訊的代理所需的所有相關信息。 (Inherited from MarshalByRefObject) |
Dispose() | 釋放由 Timer 的當前實例使用的全部資源。 |
Dispose(WaitHandle) | 釋放 Timer 的當前實例使用的全部資源並在釋放完計時器時發出信號。 |
Equals(Object) | 肯定指定的對象是否等於當前對象。 (Inherited from Object) |
GetHashCode() | 做爲默認哈希函數。 (Inherited from Object) |
GetLifetimeService() | 檢索控制此實例的生存期策略的當前生存期服務對象。 (Inherited from MarshalByRefObject) |
GetType() | 獲取當前實例的 Type。 (Inherited from Object) |
InitializeLifetimeService() | 獲取生存期服務對象來控制此實例的生存期策略。 (Inherited from MarshalByRefObject) |
MemberwiseClone() | 建立當前 Object 的淺表副本。 (Inherited from Object) |
MemberwiseClone(Boolean) | 建立當前 MarshalByRefObject 對象的淺表副本。 (Inherited from MarshalByRefObject) |
ToString() | 返回表示當前對象的字符串。 (Inherited from Object) |