解析.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


猜你喜欢
- 环境搭建创建Maven项目。pom.xml文件中引入RocketMQ依赖。<dependencies>  
- 一、对AOP的初印象首先先给出一段比较专业的术语:在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向
- Spring注入方式可以分为三类,xml注入、注解注入、BeanDefinition注入;用法上可以分为三种,但是底层实现代码都是统一Bea
- 简介工厂方法模式是什么?为什么要有工厂方法模式,不是有了简单工厂模式了吗?两个模式都有工厂,那有什么不同呢?功工厂方式模式是怎样实现的?OK
- (注意:本文基于JDK1.8)前言任何一个容器类对象用于持有元素后,总是需要遍历元素的,即挨个去访问每个元素1次,而遍历元素,除了常规的依赖
- 工厂模式定义:提供创建对象的接口。为何使用工厂模式工厂模式是我们最常用的模式了,著名的Jive论坛,就大量使用了工厂模式,工厂模式在Java
- 1. 前言什么是特殊矩阵?C++,一般使用二维数组存储矩阵数据。在实际存储时,会发现矩阵中有许多值相同的数据或有许多零数据,且分布呈现出一定
- 旧的设计方案开发api的时候,需要先定义好接口的数据响应结果.如下是一个很简单直接的Controller实现方法及响应结果定义.@RestC
- 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何自定义一个类似热门标签那样
- ElGamal数字签名,供大家参考,具体内容如下一、实验目的学习ElGamal算法在数字签名方面的使用,掌握教科书版本的ElGamal数字签
- JAVA可以通过反射获取成员变量和静态变量的名称,局部变量就不太可能拿到了。public class Test {public static
- //十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Consol
- 效果图简单的效果图(使用开源库)[FlowLayout](“ https://github.com/hongyangAndroid/Flow
- 常见尺寸单位Android开发中的常用尺寸单位有如下几种:dp (dip)pxptinchsp算不知道确切含义,相信对于以上这几种尺寸单位大
- 下面是我根据海康官方文档代码,放到VS 2022 版本中调试通过后的代码:#include <stdio.h>#include
- 发现问题在一个数据列表中我用了Linq GroupBy 和OrderBy。 排序在本机正常使用,发到测试后排序死活不对,总以为是程序问题。于
- list.remove最近做项目的过程中,需要用到list.remove()方法,结果发现两个有趣的坑,经过分析后找到原因,记录一下跟大家分
- 一、什么是IO流输入流和输出流。输入流:数据从数据源(文件)到程序(内存)的路径输出流:数据从程序(内存)到数据源(文件)的路径二、常用的文
- 前言在Android开发过程中,不管是写Demo还是实战项目中,都会打印一些日志用于记录数据,调试来着,Android中的日志工具类是Log
- 本文实例讲述了C#实现将窗体固定在显示器的左上角且不能移动的方法。分享给大家供大家参考。具体实现方法如下:using System;usin