相关文章推荐
完美的脸盆  ·  C# ...·  昨天    · 
风流倜傥的饭卡  ·  从Pandas ...·  1 年前    · 
挂过科的大蒜  ·  torch代码解析 ...·  2 年前    · 
坚强的遥控器  ·  java 读取dat文件-掘金·  2 年前    · 

它们都可以用于在指定时间间隔后执行代码。然而,它们在实现和用法上有一些差异。
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。但请注意,当执行回调方法较为复杂或耗时时,任何定时器都可能受到回调方法本身执行时间的影响,所以在选择定时器时,也要结合实际场景进行评估。

##### 愿你一寸一寸地攻城略地,一点一点地焕然一新 #####