自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# CheckStatus()方法

開發(fā) 后端
這里介紹C# CheckStatus()方法,以及介紹調(diào)用Timer.Dispose()方法刪除了timer對(duì)象,主線程于是跳出循環(huán),終止程序。

C#語(yǔ)言還是比較常見的東西,這里我們主要介紹C# CheckStatus()方法,包括介紹設(shè)置一個(gè)定時(shí)器,定時(shí)執(zhí)行用戶指定的函數(shù)等方面。

Timer類:設(shè)置一個(gè)定時(shí)器,定時(shí)執(zhí)行用戶指定的函數(shù)。

定時(shí)器啟動(dòng)后,系統(tǒng)將自動(dòng)建立一個(gè)新的線程,執(zhí)行用戶指定的函數(shù)。

  1. Timer timer = new Timer(timerDelegate, s,1000, 1000);  
  2. // ***個(gè)參數(shù):指定了TimerCallback 委托,表示要執(zhí)行的方法;  
  3. // 第二個(gè)參數(shù):一個(gè)包含回調(diào)方法要使用的信息的對(duì)象,或者為空引用;  
  4. // 第三個(gè)參數(shù):延遲時(shí)間——計(jì)時(shí)開始的時(shí)刻距現(xiàn)在的時(shí)間,單位是毫秒,
    指定為“0”表示立即啟動(dòng)計(jì)時(shí)器;  
  5. // 第四個(gè)參數(shù):定時(shí)器的時(shí)間間隔——計(jì)時(shí)開始以后,每隔這么長(zhǎng)的一段時(shí)間,
    TimerCallback所代表的方法將被調(diào)用一次,單位也是毫秒。
    指定 Timeout.Infinite 可以禁用定期終止。 

Timer.Change()方法:修改定時(shí)器的設(shè)置。(這是一個(gè)參數(shù)類型重載的方法)使用示例:timer.Change(1000,2000);

Timer類的程序示例:

  1. using System;  
  2. using System.Threading;  
  3.  
  4. namespace ThreadExample  
  5. {  
  6. class TimerExampleState   
  7. {  
  8. public int counter = 0;  
  9. public Timer tmr;  
  10. }  
  11.  
  12. class App   
  13. {  
  14. public static void Main()  
  15. {  
  16. TimerExampleState s = new TimerExampleState();  
  17.  
  18. //創(chuàng)建代理對(duì)象TimerCallback,該代理將被定時(shí)調(diào)用  
  19. TimerCallback timerDelegate = new TimerCallback(CheckStatus);  
  20.  
  21. //創(chuàng)建一個(gè)時(shí)間間隔為1s的定時(shí)器  
  22. Timer timer = new Timer(timerDelegate, s,1000, 1000);  
  23. s.tmr = timer;  
  24.  
  25. //主線程停下來(lái)等待Timer對(duì)象的終止  
  26. while(s.tmr != null)  
  27. Thread.Sleep(0);  
  28. Console.WriteLine("Timer example done.");  
  29. Console.ReadLine();  
  30. }  
  31.  
  32. //下面是被定時(shí)調(diào)用的方法  
  33. static void CheckStatus(Object state)  
  34. {  
  35. TimerExampleState s =(TimerExampleState)state;  
  36. s.counter++;  
  37. Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter);  
  38.  
  39. if(s.counter == 5)  
  40. {  
  41. //使用Change方法改變了時(shí)間間隔  
  42. (s.tmr).Change(10000,2000);  
  43. Console.WriteLine("changed");  
  44. }  
  45.  
  46. if(s.counter == 10)  
  47. {  
  48. Console.WriteLine("disposing of timer");  
  49. s.tmr.Dispose();  
  50. s.tmr = null;  
  51. }  
  52. }  
  53. }  

程序首先創(chuàng)建了一個(gè)定時(shí)器,它將在創(chuàng)建1秒之后開始每隔1秒調(diào)用一次C# CheckStatus()方法,當(dāng)調(diào)用5次以后,在C# CheckStatus()方法中修改了時(shí)間間隔為2秒,并且指定在10秒后重新開始。當(dāng)計(jì)數(shù)達(dá)到10次,調(diào)用Timer.Dispose()方法刪除了timer對(duì)象,主線程于是跳出循環(huán),終止程序。

【編輯推薦】

  1. C# lock關(guān)鍵字?jǐn)⑹?/FONT>
  2. C#.Net FrameWork簡(jiǎn)介
  3. C# new和override簡(jiǎn)單描述
  4. C#值類型和引用類型淺談
  5. C#標(biāo)識(shí)符簡(jiǎn)單分析
責(zé)任編輯:佚名 來(lái)源: CSDN論壇
相關(guān)推薦

2009-08-17 17:40:53

C# GetAllCu

2009-08-26 09:50:08

C# GreetPeo

2009-08-31 18:05:14

C#調(diào)用WalkTre

2009-08-25 17:07:27

C#虛擬方法

2009-09-07 10:37:32

C#執(zhí)行Finaliz

2009-08-19 14:42:21

C#重載LoadPos

2009-08-25 16:16:43

C# oledbcon

2009-08-31 16:33:28

C#調(diào)用Dispose

2009-08-14 10:59:35

C#分部方法

2009-09-11 09:15:06

C# get方法

2009-08-14 17:58:05

C#接口方法

2009-08-20 16:15:19

C# 匿名方法

2009-08-10 17:36:17

C#擴(kuò)展方法

2009-09-10 14:52:55

C# get

2009-08-19 17:00:07

C#實(shí)現(xiàn)PrintPa

2009-08-24 16:08:45

C# DrawStri

2009-08-20 14:22:17

C#實(shí)現(xiàn) Contro

2009-08-20 16:28:45

C#匿名方法

2009-09-09 13:31:15

C# TextBox

2009-09-01 11:04:59

C#調(diào)用擴(kuò)展方法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)