Timer.Interval 属性

定义

获取或设置引发 Elapsed 事件的间隔(以毫秒为单位)。

C#
public double Interval { get; set; }
C#
[System.Timers.TimersDescription("TimerInterval")]
public double Interval { get; set; }
C#
[System.Timers.TimersDescription("TimerInterval")]
[System.ComponentModel.SettingsBindable(true)]
public double Interval { get; set; }

属性值

Elapsed 事件之间经过的时间(以毫秒为单位)。 该值必须大于零,并且必须小于或等于 Int32.MaxValue。 默认值为 100 毫秒。

属性

例外

间隔小于或等于零。

- 或 -

间隔大于 Int32.MaxValue,并且计时器当前已启用。 (如果当前未启用计时器,则在启用它之前不会引发异常。)

示例

以下示例实例化一个 Timer 对象,该 Timer.Elapsed 对象每两秒 (2000 毫秒) 触发一次事件,为事件设置事件处理程序,并启动计时器。 事件处理程序会在每次引发属性时显示该属性的值 ElapsedEventArgs.SignalTime

C#
using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}
// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM

注解

使用 Interval 属性确定触发事件的频率 ElapsedTimer由于 类依赖于系统时钟,因此其分辨率与系统时钟相同。 这意味着, Elapsed 如果 Interval 属性小于系统时钟的分辨率,事件将按系统时钟的分辨率定义的时间间隔触发。 以下示例将 Interval 属性设置为 5 毫秒。 在系统时钟的分辨率约为 15 毫秒的 Windows 系统上运行时,事件大约每 15 毫秒触发一次,而不是每 5 毫秒触发一次。

备注

所使用的系统时钟与 GetTickCount 使用的时钟相同,不受 timeBeginPeriodtimeEndPeriod 所做的更改的影响。

C#
using System;
using System.IO;
using System.Collections.Generic;
using System.Timers;

public class Example
{
   private static Timer aTimer;
   private static List<String> eventlog;
   private static int nEventsFired = 0;
   private static DateTime previousTime;
       
   public static void Main()
   {
        eventlog = new List<String>();
        
        StreamWriter sr = new StreamWriter(@".\Interval.txt");
        // Create a timer with a five millisecond interval.
        aTimer = new Timer(5);
        aTimer.Elapsed += OnTimedEvent;
        // Hook up the Elapsed event for the timer. 
        aTimer.AutoReset = true;
        sr.WriteLine("The timer should fire every {0} milliseconds.", 
                     aTimer.Interval);
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program... ");
        Console.ReadLine();
        foreach (var item in eventlog)
           sr.WriteLine(item);
        sr.Close();
        Console.WriteLine("Terminating the application...");
   }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        eventlog.Add(String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})", 
                                   e.SignalTime, 
                                   nEventsFired++ == 0 ? 
                                      0.0 : (e.SignalTime - previousTime).TotalMilliseconds));
        previousTime = e.SignalTime;
        if (nEventsFired == 20) {
           Console.WriteLine("No more events will fire...");
           aTimer.Enabled = false;
        }
    }
}
// The example writes output like the following to a file:
//       The timer should fire every 5 milliseconds.
//       Elapsed event at 08:42:49.370344 (0)
//       Elapsed event at 08:42:49.385345 (15.0015)
//       Elapsed event at 08:42:49.400347 (15.0015)
//       Elapsed event at 08:42:49.415348 (15.0015)
//       Elapsed event at 08:42:49.430350 (15.0015)
//       Elapsed event at 08:42:49.445351 (15.0015)
//       Elapsed event at 08:42:49.465353 (20.002)
//       Elapsed event at 08:42:49.480355 (15.0015)
//       Elapsed event at 08:42:49.495356 (15.0015)
//       Elapsed event at 08:42:49.510358 (15.0015)
//       Elapsed event at 08:42:49.525359 (15.0015)
//       Elapsed event at 08:42:49.540361 (15.0015)
//       Elapsed event at 08:42:49.555362 (15.0015)
//       Elapsed event at 08:42:49.570364 (15.0015)
//       Elapsed event at 08:42:49.585365 (15.0015)
//       Elapsed event at 08:42:49.605367 (20.002)
//       Elapsed event at 08:42:49.620369 (15.0015)
//       Elapsed event at 08:42:49.635370 (15.0015)
//       Elapsed event at 08:42:49.650372 (15.0015)
//       Elapsed event at 08:42:49.665373 (15.0015)

如果应用需要比类或系统时钟提供的 Timer 分辨率更高的分辨率,请使用高分辨率多媒体计时器;请参阅 如何:使用High-Resolution计时器

如果间隔是在 启动之后 Timer 设置的,则重置计数。 例如,如果将间隔设置为 5 秒,然后将 属性设置为 Enabledtrue,则计数从设置的时间 Enabled 开始。 如果在 count 为 3 秒时将间隔重置为 10 秒,则会Elapsed在 设置为 trueEnabled 13 秒首次引发事件。

如果 Enabled 设置为 trueAutoReset 设置为 false,则 Timer 仅引发 Elapsed 一次事件,即间隔过后的第一次。 Enabled 然后将 设置为 false

备注

如果 EnabledAutoReset 都设置为 false,并且之前已启用计时器,则设置 Interval 属性会导致 Elapsed 引发事件一次,就像属性 Enabled 已设置为 true一样。 若要设置间隔而不引发事件,可以暂时将 Enabled 属性设置为 true,将 属性设置为 Interval 所需的时间间隔,然后立即将 Enabled 属性设置回 false

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅