欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

解析.NET中幾種Timer的使用

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

這篇博客將梳理一下.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開始執(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    開啟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)長(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í)或者工作能帶來一定的幫助,同時(shí)也希望多多支持我們!

上一篇:C#批量插入數(shù)據(jù)到Sqlserver中的三種方式

欄    目:C#教程

下一篇:詳解C#批量插入數(shù)據(jù)到Sqlserver中的四種方式

本文標(biāo)題:解析.NET中幾種Timer的使用

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6097.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有