欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

[C#].NET中幾種Timer的使用實(shí)例

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

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

1. System.Threading.Timer

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

callback委托將會(huì)在period時(shí)間間隔內(nèi)重復(fù)執(zhí)行,state參數(shù)可以傳入想在callback委托中處理的對象,dueTime標(biāo)識多久后callback開始執(zhí)行,period標(biāo)識多久執(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事件的時(shí)間間隔;

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定期任務(wù)是在ThreadPool的線程中執(zhí)行的。

3. System.Windows.Forms.Timer

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

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

Enabled  用于Start/Stop Timer;

Start    開啟Timer

Stop    停止Timer

使用System.Windows.Forms.Timer來更新窗體中Label內(nèi)時(shí)間,

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í)行的事件線程與主窗體的線程是同一個(gè),并沒有創(chuàng)建新線程(或者使用ThreadPool中線程)來更新UI。下面將代碼做一個(gè)改動(dòng),使用System.Timers.Timer來更新UI上的時(shí)間,代碼如下,

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}");
 };
}

很熟悉的一個(gè)錯(cuò)誤。因?yàn)長abel是由UI線程創(chuàng)建的,所以對其進(jìn)行修改需要在UI線程中進(jìn)行。System.Timers.Timer中Elasped執(zhí)行是在ThreadPool中新創(chuàng)建的線程中執(zhí)行的。所以會(huì)有上面的錯(cuò)誤。

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í)行是在主線程中進(jìn)行的。

使用DispatcherTimer時(shí)有一點(diǎn)需要注意,因?yàn)镈ispatcherTimer的Tick事件是排在Dispatcher隊(duì)列中的,當(dāng)系統(tǒng)在高負(fù)荷時(shí),不能保證在Interval時(shí)間段執(zhí)行,可能會(huì)有輕微的延遲,但是絕對可以保證Tick的執(zhí)行不會(huì)早于Interval設(shè)置的時(shí)間。如果對Tick執(zhí)行時(shí)間準(zhǔn)確性高可以設(shè)置DispatcherTimer的priority。例如:

DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Send);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#方法中參數(shù)ref和out詳解

    C#方法中參數(shù)ref和out詳解

    這篇文章主要為大家詳細(xì)介紹了C#方法中參數(shù)ref和out的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • .NET(C#):Emit創(chuàng)建異常處理的方法

    .NET(C#):Emit創(chuàng)建異常處理的方法

    .NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下
    2013-04-04
  • C# Email郵件發(fā)送功能 找回或重置密碼功能

    C# Email郵件發(fā)送功能 找回或重置密碼功能

    這篇文章主要為大家詳細(xì)介紹了C# Email郵件發(fā)送功能,找回或重置密碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Unity 按鈕事件封裝操作(EventTriggerListener)

    Unity 按鈕事件封裝操作(EventTriggerListener)

    這篇文章主要介紹了Unity 按鈕事件封裝操作(EventTriggerListener),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#檢測是否有u盤插入的方法

    C#檢測是否有u盤插入的方法

    這篇文章主要介紹了C#檢測是否有u盤插入的方法,涉及C#操作硬件的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹

    C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹

    這篇文章介紹了C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#知識整理

    C#知識整理

    本文主要介紹了C#的相關(guān)知識。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • C#之HttpClient的同步使用方式

    C#之HttpClient的同步使用方式

    這篇文章主要介紹了C#之HttpClient的同步使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • C#清除字符串內(nèi)空格的方法

    C#清除字符串內(nèi)空格的方法

    這篇文章主要介紹了C#清除字符串內(nèi)空格的方法,是C#操作字符串非常實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • C#實(shí)現(xiàn)推送釘釘消息的方法示例

    C#實(shí)現(xiàn)推送釘釘消息的方法示例

    這篇文章主要介紹了C#實(shí)現(xiàn)推送釘釘消息的方法,結(jié)合實(shí)例形式分析了C#使用釘釘API實(shí)現(xiàn)消息推送的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-02-02

最新評論