Timer Constructor (Double)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the Timer class, and sets the Interval property to the specified number of milliseconds.

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

public Timer(
	double interval


Type: System.Double

The time, in milliseconds, between events. The value must be greater than zero and less than or equal to Int32.MaxValue.

Exception Condition

The value of the interval parameter is less than or equal to zero, or greater than Int32.MaxValue.

This constructor sets the Interval property of the new timer instance, but does not enable the timer.

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.Threading.Tasks;
using System.Timers;

class Example
   static void Main()
      Timer timer = new Timer(1000);
      timer.Elapsed += async ( sender, e ) => await HandleTimer();
      Console.Write("Press any key to exit... ");

   private static Task HandleTimer()
     Console.WriteLine("\nHandler not implemented..." );
     throw new NotImplementedException();
// The example displays output like the following:
//   Press any key to exit...
//   Handler not implemented...
//   Unhandled Exception: System.NotImplementedException: The method or operation is not implemented.
//      at Example.HandleTimer()
//      at Example.<<Main>b__0>d__2.MoveNext()
//   --- End of stack trace from previous location where exception was thrown ---
//      at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c__DisplayClass2.<ThrowAsync>b__5(Object state)
//      at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
//      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
//      at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
//      at System.Threading.ThreadPoolWorkQueue.Dispatch()

.NET Framework
Available since 1.1
Return to top