C# system.Threading.Timer和system.Timers中的定时器
它们都可以用于在指定时间间隔后执行代码。然而,它们在实现和用法上有一些差异。
System.Threading.Timer:
System.Threading.Timer 是基于线程的定时器,它属于 System.Threading 命名空间。它使用 ThreadPool 线程来执行定时操作。以下是关于 System.Threading.Timer 的一些重要特点:
构造函数:Timer(TimerCallback callback, object state, int dueTime, int period)
TimerCallback 是一个委托,它指向要定时执行的方法。state 是传递给回调方法的对象参数。
dueTime 表示创建定时器后首次执行回调方法的延迟时间(以毫秒为单位)。
period 表示回调方法的执行间隔(以毫秒为单位)。如果将其设置为 0,它只会在 dueTime 后执行一次。
示例:
using System;
using System.Threading;
class Program
{
static void Main()
{
TimerCallback callback = DoSomething;
Timer timer = new Timer(callback, null, 0, 1000);
Console.WriteLine("Press any key to stop the timer.");
Console.ReadKey();
// Dispose the timer to stop it properly.
timer.Dispose();
}
static void DoSomething(object state)
{
Console.WriteLine("Doing something...");
}
}
System.Timers.Timer:
System.Timers.Timer 是基于事件的定时器,它属于 System.Timers 命名空间。它使用基于底层计时器的实现,而不是线程池线程。以下是关于 System.Timers.Timer 的一些重要特点:
构造函数:Timer(double interval)
interval 表示定时器的执行间隔(以毫秒为单位)。
示例:
using System;
using System.Timers;
class Program
{
static void Main()
{
Timer timer = new Timer(1000); // 1 second interval
timer.Elapsed += Timer_Elapsed;
timer.Start();
Console.WriteLine("Press any key to stop the timer.");
Console.ReadKey();
timer.Stop();
timer.Dispose();
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Doing something...");
}
}
2 / 2
在 C# 中,有两种常用的定时器类,分别是 System.Threading.Timer 和 System.Timers.Timer,它们都可以用于在指定时间间隔后执行代码。然而,它们在实现和用法上有一些差异。
System.Threading.Timer:
System.Threading.Timer 是基于线程的定时器,它属于 System.Threading 命名空间。它使用 ThreadPool 线程来执行定时操作。以下是关于 System.Threading.Timer 的一些重要特点:
构造函数:Timer(TimerCallback callback, object state, int dueTime, int period)
TimerCallback 是一个委托,它指向要定时执行的方法。state 是传递给回调方法的对象参数。
dueTime 表示创建定时器后首次执行回调方法的延迟时间(以毫秒为单位)。
period 表示回调方法的执行间隔(以毫秒为单位)。如果将其设置为 0,它只会在 dueTime 后执行一次。
示例:
csharp
Copy code
using System;
using System.Threading;
class Program
{
static void Main()
{
TimerCallback callback = DoSomething;
Timer timer = new Timer(callback, null, 0, 1000);
Console.WriteLine("Press any key to stop the timer.");
Console.ReadKey();
// Dispose the timer to stop it properly.
timer.Dispose();
}
static void DoSomething(object state)
{
Console.WriteLine("Doing something...");
}
}
System.Timers.Timer:
System.Timers.Timer 是基于事件的定时器,它属于 System.Timers 命名空间。它使用基于底层计时器的实现,而不是线程池线程。以下是关于 System.Timers.Timer 的一些重要特点:
构造函数:Timer(double interval)
interval 表示定时器的执行间隔(以毫秒为单位)。
示例:
csharp
Copy code
using System;
using System.Timers;
class Program
{
static void Main()
{
Timer timer = new Timer(1000); // 1 second interval
timer.Elapsed += Timer_Elapsed;
timer.Start();
Console.WriteLine("Press any key to stop the timer.");
Console.ReadKey();
timer.Stop();
timer.Dispose();
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Doing something...");
}
}
差异总结:
实现方式:
System.Threading.Timer 使用 ThreadPool 线程执行定时操作。
System.Timers.Timer 使用基于底层计时器的实现。
精度:
System.Threading.Timer 相对精确,但受到线程池的限制,可能会有轻微的延迟。
System.Timers.Timer 提供更高的精确度。
回调方法:
System.Threading.Timer 使用 TimerCallback 委托来执行回调方法。
System.Timers.Timer 使用事件模型(Elapsed 事件)来执行回调方法。
线程安全性:
System.Threading.Timer 需要开发人员自行处理可能出现的线程同步问题。
System.Timers.Timer 本身是线程安全的,因为它使用了基于事件的模型。
根据需要选择合适的定时器类,如果需要更高的精确度和线程安全性,可以优先考虑 System.Timers.Timer。但请注意,当执行回调方法较为复杂或耗时时,任何定时器都可能受到回调方法本身执行时间的影响,所以在选择定时器时,也要结合实际场景进行评估。
C# system.Threading.Timer和system.Timers中的定时器的更多相关文章
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用
System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...
- C# 计时器用法(DispatcherTimer、System.Timers.Timer、System.Threading.Timer)
首先,我觉得三种计时器最大的区别是:DispatcherTimer触发的内容会直接转到主线程去执行(耗时操作会卡住主线程),另外两个则是在副线程执行,如果需要修改界面,则需要手动转到主线程. Disp ...
- C# System.Threading.Timer 详解及示例
前言 定时器功能在日常开发中也是比较常用的,在 .Net 中实际上总共有五种定时器,分别是:System.Timers.Timer.System.Threading.Timer.System.Wind ...
- System.Threading.Timer的使用技巧
转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...
- .NET中System.Diagnostics.Stopwatch、System.Timers.Timer、System.Threading.Timer 的区别
1.System.Diagnostics.Stopwatch Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间. 在典型的 Stopwatch 方案中,先调用 ...
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
- c# 多线程之-- System.Threading Timer的使用
作用:每隔多久去执行线程里的方法. class ThreadTimerDemo { static void Main(string[] args) { // Create an AutoResetEv ...
- System.Threading.Timer使用心得
System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...
- System.Threading.Timer如何正确地被Dispose
System.Threading.Timer是.NET中一个定时触发事件处理方法的类(本文后面简称Timer),它背后依靠的是.NET的线程池(ThreadPool),所以当Timer在短时间内触发了 ...
随机推荐
- manim边学边做--相机Camera简介
在Manim中,Camera是实现动画效果的重要工具之一. 它就像我们观察动画的 眼睛,通过控制相机的位置.角度和视野,可以创造出丰富多样的视觉效果. Manim社区版本中提供了多种Camera类型, ...
- AI图像翻译
在当今互联互通的世界中,快速准确地翻译图像中的文本的能力非常宝贵.无论您是商务人士.教育工作者.设计师还是内容创作者,Visual Paradigm Online 的 AI 图像翻译器都能提供强大的解 ...
- Mermaid Live Editor 如何调整方向
在 Mermaid Live Editor 中,默认情况下,流程图和序列图等都是按照从左到右的方向来布局的.但是,如果你想要创建一个从上到下(纵向)布局的图形,你可以使用 TD(Top-Down,即从 ...
- vue学习二(过滤器)
过滤器常用户来处理文本格式化的操作 过滤器还可以用在两个地方:花括号和v-bind 表达式 1.全局过滤器 {{user.gender|gfilter}} Vue.filter("gfil ...
- Windows下Cmake+VS2017+OpenSceneGraph环境搭建及采坑
一.资料准备 1.OSG源码官网下载地址:http://www.openscenegraph.org/index.php/download-section/stable-releases或者OSGCh ...
- Docker镜像相关-查看镜像信息
主要涉及Docker镜像的ls.tag和inspect子命令. 使用images命令列出镜像 使用docker images或docker image ls命令可以列出本地主机上已有镜像的基本信息,字 ...
- RANSAC---从直线拟合到特征匹配去噪
Ransac全称为Random Sample Consensus,随机一致性采样.该方法是一种十分高效的数据拟合方法.我们通过最简单的拟合直线任务来了解这种方法思路,继而扩展到特征点匹配中的误点剔除问 ...
- MaxKB+Ollama 离线部署
主题:在 Centos7 环境部署 MaxKB 以及 Ollama 实现基于离线大模型的的小助手调用. 选择离线部署的原因:原计划是打算直接使用 1Panel 进行 MaxKB 和 Ollama 一键 ...
- Oracle AI应用的LLM模型典型配置
最近在做一些基于Oracle的一些AI应用测试工作,AI肯定离不开配置LLM相关,虽然是简单配置类,但实际还是遇到一些卡点,记录下来供今后参考. 1.配置Embedding模型 2.特殊语法传参JSO ...
- [每日算法 - 华为机试] 剑指 Offer 10- II. 青蛙跳台阶问题
入口 力扣https://leetcode.cn/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/ 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶.求该 ...