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

解析.NET中幾種Timer的使用

 更新時(shí)間:2016年12月13日 11:56:46   作者:Yang-Fei  
本文主要對(duì).NET中4個(gè)Timer類,及其用法進(jìn)行梳理,具有很好參考價(jià)值,需要的朋友一起來(lái)看下吧

這篇博客將梳理一下.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委托中處理的對(duì)象,dueTime標(biāo)識(shí)多久后callback開(kāi)始執(zhí)行,period標(biāo)識(shí)多久執(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,
);
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    開(kāi)啟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    開(kāi)啟Timer

Stop    停止Timer

使用System.Windows.Forms.Timer來(lái)更新窗體中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è),并沒(méi)有創(chuàng)建新線程(或者使用ThreadPool中線程)來(lái)更新UI。下面將代碼做一個(gè)改動(dòng),使用System.Timers.Timer來(lái)更新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)長(zhǎng)abel是由UI線程創(chuàng)建的,所以對(duì)其進(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ì)有輕微的延遲,但是絕對(duì)可以保證Tick的執(zhí)行不會(huì)早于Interval設(shè)置的時(shí)間。如果對(duì)Tick執(zhí)行時(shí)間準(zhǔn)確性高可以設(shè)置DispatcherTimer的priority。例如:

DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Send);

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • Unity實(shí)現(xiàn)跑馬燈抽獎(jiǎng)效果

    Unity實(shí)現(xiàn)跑馬燈抽獎(jiǎng)效果

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)跑馬燈抽獎(jiǎng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C# 修改文件的創(chuàng)建、修改和訪問(wèn)時(shí)間的示例

    C# 修改文件的創(chuàng)建、修改和訪問(wèn)時(shí)間的示例

    這篇文章主要介紹了C#實(shí)現(xiàn)修改文件的創(chuàng)建、修改和訪問(wèn)時(shí)間的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • 基于C#制作一個(gè)飛機(jī)大戰(zhàn)小游戲的全過(guò)程

    基于C#制作一個(gè)飛機(jī)大戰(zhàn)小游戲的全過(guò)程

    飛機(jī)大戰(zhàn)小游戲詳細(xì)大家都不陌生,下面這篇文章主要給大家介紹了關(guān)于基于C#制作一個(gè)飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • 解析c#操作excel后關(guān)閉excel.exe的方法

    解析c#操作excel后關(guān)閉excel.exe的方法

    C#和Asp.net下excel進(jìn)程一被打開(kāi),有時(shí)就無(wú)法關(guān)閉,尤其是website.對(duì)關(guān)閉該進(jìn)程有過(guò)GC、release等方法,但這些方法并不是在所有情況下均適用
    2013-07-07
  • C#基礎(chǔ)之泛型

    C#基礎(chǔ)之泛型

    泛型是 2.0 版 C# 語(yǔ)言和公共語(yǔ)言運(yùn)行庫(kù) (CLR) 中的一個(gè)新功能。接下來(lái)通過(guò)本文給大家介紹c#基礎(chǔ)之泛型,感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08
  • C#使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成

    C#使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成

    這篇文章主要為大家詳細(xì)介紹了C#如何使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • C#實(shí)現(xiàn)加密bat文件的示例詳解

    C#實(shí)現(xiàn)加密bat文件的示例詳解

    這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)加密bat文件的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01
  • C#/Java連接sqlite與使用技巧

    C#/Java連接sqlite與使用技巧

    無(wú)意中發(fā)現(xiàn)的,C#/Java連接sqlite與使用技巧??戳讼?,還挺不錯(cuò)的。與大家分享一下。
    2013-04-04
  • C#比較二個(gè)數(shù)組并找出相同或不同元素的方法

    C#比較二個(gè)數(shù)組并找出相同或不同元素的方法

    這篇文章主要介紹了C#比較二個(gè)數(shù)組并找出相同或不同元素的方法,涉及C#針對(duì)數(shù)組的交集、補(bǔ)集等集合操作相關(guān)技巧,非常簡(jiǎn)單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • C#使用async和await實(shí)現(xiàn)異步編程

    C#使用async和await實(shí)現(xiàn)異步編程

    本文詳細(xì)講解了C#使用async和await實(shí)現(xiàn)異步編程的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評(píng)論

巴青县| 萝北县| 辽阳县| 新泰市| 祁阳县| 长岛县| 扶风县| 衡阳县| 三门县| 英山县| 横山县| 综艺| 克什克腾旗| 定陶县| 唐河县| 资溪县| 仙居县| 石景山区| 梅州市| 武穴市| 庆元县| 黑山县| 丰台区| 荃湾区| 凌云县| 大荔县| 灌云县| 双城市| 黄石市| 曲阳县| 南澳县| 镇沅| 拜泉县| 自治县| 高尔夫| 长乐市| 临高县| 拉萨市| 岱山县| 临安市| 城固县|