解析.NET中几种Timer的使用
作者:Yang-Fei 时间:2023-05-21 17:27:53
这篇博客将梳理一下.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);
来源:http://www.cnblogs.com/yang-fei/p/6169089.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaWeb开发之使用jQuery与Ajax实现动态联级菜单效果
![](https://img.aspxhome.com/file/2023/6/60466_0s.png)
Java Swing组件编程之JTable表格用法实例详解
![](https://img.aspxhome.com/file/2023/5/76195_0s.jpg)
如何利用Jackson序列化忽略指定类型的属性详解
C# javascript 读写Cookie的方法
Android中实现TCP和UDP传输实例
![](https://img.aspxhome.com/file/2023/4/114294_0s.png)
C#操作DataGridView获取或设置当前单元格的内容
Java8中的LocalDateTime和Date一些时间操作方法
Android SharedPreferences存储用法详解
![](https://img.aspxhome.com/file/2023/8/83528_0s.gif)
maven多个仓库查询的优先级顺序案例讲解
关于Spring Boot获取bean的3种方式
![](https://img.aspxhome.com/file/2023/2/111442_0s.png)
Qt GUI图形图像开发之Qt表格控件QTableView简单使用方法及QTableView与QTableWidget区别
![](https://img.aspxhome.com/file/2023/4/117774_0s.jpg)
Android中使用ZXing生成二维码(支持添加Logo图案)
![](https://img.aspxhome.com/file/2023/1/110681_0s.png)
Java中的ArrayList容量及扩容方式
Java实现图片倒影的源码实例内容
![](https://img.aspxhome.com/file/2023/2/65162_0s.jpg)
C# 扩展方法小结
C#通过指针读取文件的方法
Android编程之自定义锁屏实例分析
springboot 防止重复请求防止重复点击的操作
![](https://img.aspxhome.com/file/2023/6/71196_0s.png)
Android RecyclerView添加搜索过滤器的示例代码
![](https://img.aspxhome.com/file/2023/8/108278_0s.gif)