最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

[C#].NET中幾種Timer的使用實例

 更新時間:2016年12月13日 14:07:50   作者:Yang-Fei  
本篇文章主要介紹了.NET中幾種Timer的使用,具有一定的參考價值,有興趣的可以了解一下。

這篇博客將梳理一下.NET中4個Timer類,及其用法。

1. System.Threading.Timer

public Timer(TimerCallback callback, object state, int dueTime, int period);

callback委托將會在period時間間隔內重復執(zhí)行,state參數(shù)可以傳入想在callback委托中處理的對象,dueTime標識多久后callback開始執(zhí)行,period標識多久執(zhí)行一次callback。

using System.Threading;
// System.Threading.Timer

Timer timer = new Timer(delegate
{
 Console.WriteLine($"Timer Thread: {Thread.CurrentThread.ManagedThreadId}");

 Console.WriteLine($"Is Thread Pool: {Thread.CurrentThread.IsThreadPoolThread}");

 Console.WriteLine("Timer Action.");
},
null,
2000,
1000
);

Console.WriteLine("Main Action.");
Console.WriteLine($"Main Thread: {Thread.CurrentThread.ManagedThreadId}");

Console.ReadLine();

Timer回掉方法執(zhí)行是在另外ThreadPool中一條新線程中執(zhí)行的。

2. System.Timers.Timer

System.Timers.Timer和System.Threading.Timer相比,提供了更多的屬性,

Interval  指定執(zhí)行Elapsed事件的時間間隔;

Elapsed  指定定期執(zhí)行的事件;

Enabled  用于Start/Stop Timer;

Start    開啟Timer

Stop    停止Timer

System.Timers.Timer timer = new System.Timers.Timer();

timer.Interval = 500;

timer.Elapsed += delegate
{
 Console.WriteLine($"Timer Thread: {Thread.CurrentThread.ManagedThreadId}");

 Console.WriteLine($"Is Thread Pool: {Thread.CurrentThread.IsThreadPoolThread}");

 Console.WriteLine("Timer Action");

 timer.Stop();
};

timer.Start();

Console.WriteLine("Main Action.");
Console.WriteLine($"Main Thread: {Thread.CurrentThread.ManagedThreadId}");

Console.ReadLine();

Timer Elapsed定期任務是在ThreadPool的線程中執(zhí)行的。

3. System.Windows.Forms.Timer

Interval  指定執(zhí)行Elapsed事件的時間間隔;

Tick       指定定期執(zhí)行的事件;

Enabled  用于Start/Stop Timer;

Start    開啟Timer

Stop    停止Timer

使用System.Windows.Forms.Timer來更新窗體中Label內時間,

using System.Windows.Forms;
public Form1()
{
 InitializeComponent();
 this.Load += delegate
 {
  Timer timer = new Timer();

  timer.Interval = 500;

  timer.Tick += delegate
  {
   System.Diagnostics.Debug.WriteLine($"Timer Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");

   System.Diagnostics.Debug.WriteLine($"Is Thread Pool: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}");

   this.lblTimer.Text = DateTime.Now.ToLongTimeString();
  };

  timer.Start();

  System.Diagnostics.Debug.WriteLine($"Main Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
 };
}

Timer Tick事件中執(zhí)行的事件線程與主窗體的線程是同一個,并沒有創(chuàng)建新線程(或者使用ThreadPool中線程)來更新UI。下面將代碼做一個改動,使用System.Timers.Timer來更新UI上的時間,代碼如下,

public Form1()
{
 InitializeComponent();

 this.Load += delegate
 {
  System.Timers.Timer timer = new System.Timers.Timer();

  timer.Interval = 500;

  timer.Elapsed += delegate
  {
   System.Diagnostics.Debug.WriteLine($"Timer Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");

   System.Diagnostics.Debug.WriteLine($"Is Thread Pool: {System.Threading.Thread.CurrentThread.IsThreadPoolThread}");

   this.lblTimer.Text = DateTime.Now.ToLongTimeString();
  };

  timer.Start();

  System.Diagnostics.Debug.WriteLine($"Main Thread: {System.Threading.Thread.CurrentThread.ManagedThreadId}");
 };
}

很熟悉的一個錯誤。因為Label是由UI線程創(chuàng)建的,所以對其進行修改需要在UI線程中進行。System.Timers.Timer中Elasped執(zhí)行是在ThreadPool中新創(chuàng)建的線程中執(zhí)行的。所以會有上面的錯誤。

4. System.Windows.Threading.DispatcherTimer

屬性和方法與System.Windows.Forms.Timer類似。

using System.Windows.Threading;

public MainWindow()
{
 InitializeComponent();

 this.Loaded += delegate
 {
  //DispatcherTimer

  DispatcherTimer timer = new DispatcherTimer();

  timer.Interval = TimeSpan.FromSeconds(1);

  timer.Start();

  Debug.WriteLine($"Main Thread Id: {Thread.CurrentThread.ManagedThreadId}");

  timer.Tick += delegate
  {
   tbTime.Text = DateTime.Now.ToLongTimeString();

   Debug.WriteLine($"Timer Thread Id: {Thread.CurrentThread.ManagedThreadId}");

   timer.Stop();
  };
 };
}

DispatcherTimer中Tick事件執(zhí)行是在主線程中進行的。

使用DispatcherTimer時有一點需要注意,因為DispatcherTimer的Tick事件是排在Dispatcher隊列中的,當系統(tǒng)在高負荷時,不能保證在Interval時間段執(zhí)行,可能會有輕微的延遲,但是絕對可以保證Tick的執(zhí)行不會早于Interval設置的時間。如果對Tick執(zhí)行時間準確性高可以設置DispatcherTimer的priority。例如:

DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Send);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#通過屬性名稱獲取(讀取)屬性值的方法

    C#通過屬性名稱獲取(讀取)屬性值的方法

    本文主要介紹了C#通過屬性名稱獲取(讀取)屬性值的方法,并提供了簡化版代碼,具有很好的參考價值,需要的朋友可以看下
    2016-12-12
  • C#連接SQL Server的實現(xiàn)方法

    C#連接SQL Server的實現(xiàn)方法

    這篇文章主要給大家介紹了關于C#連接SQL Server的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • C# PropertyInfo類案例詳解

    C# PropertyInfo類案例詳解

    這篇文章主要介紹了C# PropertyInfo類案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • C#用Lambda和委托實現(xiàn)模板方法

    C#用Lambda和委托實現(xiàn)模板方法

    C#用Lambda和委托實現(xiàn)模板方法,需要的朋友可以參考一下
    2013-03-03
  • C#圖表算法之有向圖

    C#圖表算法之有向圖

    這篇文章介紹了C#圖表算法之有向圖,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 如何在C#中使用 CancellationToken 處理異步任務

    如何在C#中使用 CancellationToken 處理異步任務

    這篇文章主要介紹了如何在C#中使用 CancellationToken 處理異步任務,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#匿名委托和Java匿名局部內部類使用方法示例

    C#匿名委托和Java匿名局部內部類使用方法示例

    Java在嵌套類型這里提供的特性比較多,假設:Java的字節(jié)碼只支持靜態(tài)嵌套類,內部類、局部內部類和匿名局部內部類都是編譯器提供的語法糖,這個假設目前沒法驗證(看不懂字節(jié)碼),本文先來看一下C#是如何為我們提供的這種語法糖
    2013-11-11
  • DevExpress實現(xiàn)TreeList按條件隱藏節(jié)點CheckBox的方法

    DevExpress實現(xiàn)TreeList按條件隱藏節(jié)點CheckBox的方法

    這篇文章主要介紹了DevExpress實現(xiàn)TreeList按條件隱藏節(jié)點CheckBox的方法,需要的朋友可以參考下
    2014-08-08
  • C# 調用Delphi dll 實例代碼

    C# 調用Delphi dll 實例代碼

    這篇文章介紹了C# 調用Delphi dll 實例代碼,有需要的朋友可以參考一下
    2013-09-09
  • C#實現(xiàn)XML文檔的增刪改查功能示例

    C#實現(xiàn)XML文檔的增刪改查功能示例

    這篇文章主要介紹了C#實現(xiàn)XML文檔的增刪改查功能,結合實例形式分析了xml文檔的創(chuàng)建及C#針對xml文檔的加載及增刪改查等操作技巧,需要的朋友可以參考下
    2017-01-01

最新評論

孙吴县| 珠海市| 玉龙| 临泽县| 北辰区| 长春市| 贵溪市| 平和县| 孟津县| 长春市| 景洪市| 株洲市| 珲春市| 高密市| 鄂温| 麻阳| 清水县| 舞钢市| 元朗区| 禄丰县| 鞍山市| 民权县| 扶绥县| 泰安市| 万山特区| 绿春县| 赤城县| 南安市| 苗栗市| 康马县| 无极县| 普陀区| 翁牛特旗| 安宁市| 钦州市| 云南省| 全南县| 天台县| 石棉县| 定陶县| 同江市|