它们都可以用于在指定时间间隔后执行代码。然而,它们在实现和用法上有一些差异。

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中的定时器的更多相关文章

  1. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  2. 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 ...

  3. C# 计时器用法(DispatcherTimer、System.Timers.Timer、System.Threading.Timer)

    首先,我觉得三种计时器最大的区别是:DispatcherTimer触发的内容会直接转到主线程去执行(耗时操作会卡住主线程),另外两个则是在副线程执行,如果需要修改界面,则需要手动转到主线程. Disp ...

  4. C# System.Threading.Timer 详解及示例

    前言 定时器功能在日常开发中也是比较常用的,在 .Net 中实际上总共有五种定时器,分别是:System.Timers.Timer.System.Threading.Timer.System.Wind ...

  5. System.Threading.Timer的使用技巧

    转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...

  6. .NET中System.Diagnostics.Stopwatch、System.Timers.Timer、System.Threading.Timer 的区别

    1.System.Diagnostics.Stopwatch Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间. 在典型的 Stopwatch 方案中,先调用 ...

  7. System.Threading.Timer 定时器的用法

    System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此  .Net Framework 提供了5个重载的构造 ...

  8. c# 多线程之-- System.Threading Timer的使用

    作用:每隔多久去执行线程里的方法. class ThreadTimerDemo { static void Main(string[] args) { // Create an AutoResetEv ...

  9. System.Threading.Timer使用心得

    System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...

  10. System.Threading.Timer如何正确地被Dispose

    System.Threading.Timer是.NET中一个定时触发事件处理方法的类(本文后面简称Timer),它背后依靠的是.NET的线程池(ThreadPool),所以当Timer在短时间内触发了 ...

随机推荐

  1. Java、Python等接入方式对接股票数据源API接口

    为了创建一个Python项目来对接StockTV的API接口,我们可以使用requests库来发送HTTP请求,并使用websocket-client库来处理WebSocket连接.以下是一个简单的P ...

  2. 关于JS框架的一点想法

    读了几页<vue.js设计与实现>,记录几点想法: 1.Html是根本 2.无论哪个框架,不管是运行时框架,还是编译时框架,最终都要通过标签"绘制"页面 3.这个&qu ...

  3. 单页应用(SPA)是什么?

    来源:https://zhuanlan.zhihu.com/p/648113861 概述 单页应用(SPA,Single Page Application)是一种网页应用或网站的设计模式,它在浏览器中 ...

  4. 使用Win32控制台实现命名管道通信

    接收端: //server //命名管道采用基于连接的可靠传输方式,只能一对一传输 #include <windows.h> #include <iostream> #defi ...

  5. MQ 如何保证数据一致性?

    前言 上个月,我们有个电商系统出了个灵异事件:用户支付成功了,但订单状态死活不改成"已发货". 折腾了半天才定位到问题:订单服务的MQ消息,像人间蒸发一样消失了. 这个Bug让我明 ...

  6. 基于RK3568 + FPGA国产平台的多通道AD实时采集显示方案分享

    在工业控制与数据采集领域,高精度的AD采集和实时显示至关重要.今天,我们就来基于瑞芯微RK3568J + FPGA国产平台深入探讨以下,它是如何实现该功能的.适用开发环境如下: Windows开发环境 ...

  7. 【Java】操作数据库

    工具: eclipse MySQL Navicat for MySQL MySQL 连接驱动:mysql-connector-java-5.0.4-bin.jar SQL 代码 CREATE TABL ...

  8. 【Web】支持纯静态的Layuimini版本

    支持纯静态的Layuimini版本 本人做了点小小的改动,在来的基础上添加了对静态的支持. 零.起因 要做个项目,但是用的是JSP,想着用Layui,然后去找模板,发现这个Layuimini.但是这个 ...

  9. Quartz.NET - 教程 2: 作业和触发器

    译者注: 目录在这 Quartz.NET 3.x 教程 原文在这 Lesson 2: Jobs And Triggers Quartz API Quartz API 的主要接口和类如下: ISched ...

  10. .net core基础(一):安装并创建第一个webapi

    一..net介绍 .net是一个开发者平台的统称,用它可以构建多种类型的应用程序. .net平台下的开发语言:C#,F#,Visual Basic .net平台标准:.NET Standard .ne ...