Export (0) Print
Expand All

Timer.Interval Property

Gets or sets the interval at which to raise the Elapsed event.

Namespace:  System.Timers
Assembly:  System (in System.dll)

[SettingsBindableAttribute(true)]
[TimersDescriptionAttribute("TimerInterval")]
public double Interval { get; set; }

Property Value

Type: System.Double
The time, in milliseconds, between Elapsed events. The value must be greater than zero, and less than or equal to Int32.MaxValue. The default is 100 milliseconds.

ExceptionCondition
ArgumentException

The interval is less than or equal to zero.

-or-

The interval is greater than Int32.MaxValue, and the timer is currently enabled. (If the timer is not currently enabled, no exception is thrown until it becomes enabled.)

You use the Interval property to determine the frequency at which the Elapsed event is fired. Because the Timer class depends on the system clock, it has the same resolution as the system clock. This means that the Elapsed event will fire at an interval defined by the resolution of the system clock if the Interval property is less than the resolution of the system clock. The following example sets the Interval property to 5 milliseconds. When run on a Windows 7 system whose system clock has a resolution of approximately 15 milliseconds, the event fires approximately every 15 milliseconds rather than every 5 milliseconds.

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)

You can use the following code to determine the resolution of the system clock on the current system:

using System;
using System.Runtime.InteropServices;

public class Example
{
   [DllImport("kernel32.dll", SetLastError=true)]
   static extern bool GetSystemTimeAdjustment(out long lpTimeAdjustment,
                                              out long lpTimeIncrement,
                                              out bool lpTimeAdjustmentDisabled);

   public static void Main()
   {
      long timeAdjustment, timeIncrement = 0;
      bool timeAdjustmentDisabled;

      if (GetSystemTimeAdjustment(out timeAdjustment, out timeIncrement, 
                                  out timeAdjustmentDisabled)) {
         if (! timeAdjustmentDisabled)
            Console.WriteLine("System clock resolution: {0:N3} milliseconds", 
                              timeIncrement/10000.0);
         else
            Console.WriteLine("Unable to determine system clock resolution.");                     
      }
   }      
}
// The example displays output like the following: 
//        System clock resolution: 15.600 milliseconds

If your app requires greater resolution than that offered by the Timer class or the system clock, use the high-resolution multimedia timers; see How to: Use the High-Resolution Timer.

If the interval is set after the Timer has started, the count is reset. For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.

If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses. Enabled is then set to false.

NoteNote

If Enabled and AutoReset are both set to false, and the timer has previously been enabled, setting the Interval property causes the Elapsed event to be raised once, as if the Enabled property had been set to true. To set the interval without raising the event, you can temporarily set the Enabled property to true, set the Interval property to the desired time interval, and then immediately set the Enabled property back to false.

The following example instantiates a Timer object that fires its Timer.Elapsed event every two seconds (2000 milliseconds), sets up an event handler for the event, and starts the timer. The event handler displays the value of the ElapsedEventArgs.SignalTime property each time it is raised.

using System;
using System.Timers;

public class Example
{
   private static Timer aTimer;

   public static void Main()
   {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(2000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program... ");
        Console.ReadLine();
        Console.WriteLine("Terminating the application...");
   }

    private static void OnTimedEvent(Object source, 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... 
//       The Elapsed event was raised at 4/5/2014 8:48:58 PM 
//       The Elapsed event was raised at 4/5/2014 8:49:00 PM 
//       The Elapsed event was raised at 4/5/2014 8:49:02 PM 
//       The Elapsed event was raised at 4/5/2014 8:49:04 PM 
//       The Elapsed event was raised at 4/5/2014 8:49:06 PM 
//        
//       Terminating the application...

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft