解析.NET中几种Timer的使用

 更新时间:2016年12月13日 11:56:46   作者:Yang-Fei  
本文主要对.NET中4个Timer类,及其用法进行梳理,具有很好参考价值,需要的朋友一起来看下吧

这篇博客将梳理一下.NET中4个Timer类,及其用法。

1. System.Threading.Timer

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

callback委托将会在period时间间隔内重复执行,state参数可以传入想在callback委托中处理的对象,dueTime标识多久后callback开始执行,period标识多久执行一次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回掉方法执行是在另外ThreadPool中一条新线程中执行的。

2. System.Timers.Timer

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

Interval  指定执行Elapsed事件的时间间隔;

Elapsed  指定定期执行的事件;

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的线程中执行的。

3. System.Windows.Forms.Timer

Interval  指定执行Elapsed事件的时间间隔;

Tick       指定定期执行的事件;

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事件中执行的事件线程与主窗体的线程是同一个,并没有创建新线程(或者使用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线程创建的,所以对其进行修改需要在UI线程中进行。System.Timers.Timer中Elasped执行是在ThreadPool中新创建的线程中执行的。所以会有上面的错误。

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事件执行是在主线程中进行的。

使用DispatcherTimer时有一点需要注意,因为DispatcherTimer的Tick事件是排在Dispatcher队列中的,当系统在高负荷时,不能保证在Interval时间段执行,可能会有轻微的延迟,但是绝对可以保证Tick的执行不会早于Interval设置的时间。如果对Tick执行时间准确性高可以设置DispatcherTimer的priority。例如:

DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Send);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

相关文章

  • Unity实现跑马灯抽奖效果

    Unity实现跑马灯抽奖效果

    这篇文章主要为大家详细介绍了Unity实现跑马灯抽奖效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C# 修改文件的创建、修改和访问时间的示例

    C# 修改文件的创建、修改和访问时间的示例

    这篇文章主要介绍了C#实现修改文件的创建、修改和访问时间的示例,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • 基于C#制作一个飞机大战小游戏的全过程

    基于C#制作一个飞机大战小游戏的全过程

    飞机大战小游戏详细大家都不陌生,下面这篇文章主要给大家介绍了关于基于C#制作一个飞机大战小游戏的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • 解析c#操作excel后关闭excel.exe的方法

    解析c#操作excel后关闭excel.exe的方法

    C#和Asp.net下excel进程一被打开,有时就无法关闭,尤其是website.对关闭该进程有过GC、release等方法,但这些方法并不是在所有情况下均适用
    2013-07-07
  • C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧
    2016-08-08
  • C#使用PPT组件的CreateVideo方法实现视频生成

    C#使用PPT组件的CreateVideo方法实现视频生成

    这篇文章主要为大家详细介绍了C#如何使用PPT组件的CreateVideo方法实现视频生成,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下
    2023-10-10
  • C#实现加密bat文件的示例详解

    C#实现加密bat文件的示例详解

    这篇文章主要为大家详细介绍了C#如何实现加密bat文件的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-01-01
  • C#/Java连接sqlite与使用技巧

    C#/Java连接sqlite与使用技巧

    无意中发现的,C#/Java连接sqlite与使用技巧。看了下,还挺不错的。与大家分享一下。
    2013-04-04
  • C#比较二个数组并找出相同或不同元素的方法

    C#比较二个数组并找出相同或不同元素的方法

    这篇文章主要介绍了C#比较二个数组并找出相同或不同元素的方法,涉及C#针对数组的交集、补集等集合操作相关技巧,非常简单实用,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • C#使用async和await实现异步编程

    C#使用async和await实现异步编程

    本文详细讲解了C#使用async和await实现异步编程的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07

最新评论