Timer::AutoReset Property


Gets or sets a Boolean indicating whether the Timer should raise the Elapsed event only once (false) or repeatedly (true).

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

property bool AutoReset {
	bool get();
	void set(bool value);

Property Value

Type: System::Boolean

true if the Timer should raise the Elapsed event each time the interval elapses; false if it should raise the Elapsed event only once, after the first time the interval elapses. The default is true.

If the Timer is already enabled when the Start method is called, the interval is reset. If AutoReset is false, the Start method must be called in order to start the count again.

Resetting the interval affects when the Elapsed event is raised. 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 the count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after the Enabled property was set to true.

The following example creates a Timer whose Elapsed event fires after 1.5 seconds. Its event handler then displays "Hello World!" on the console.

#using <system.dll>

using namespace System;
using namespace System::Timers;

public ref class Timer2
   static System::Timers::Timer^ aTimer;

   static void Main()
      // Create a new Timer with Interval set to 1.5 seconds.
      double interval = 1500.0;
      aTimer = gcnew System::Timers::Timer(interval);

      // Hook up the event handler for the Elapsed event.
      aTimer->Elapsed += gcnew ElapsedEventHandler( OnTimedEvent );

      // Only raise the event the first time Interval elapses.
      aTimer->AutoReset = false;
      aTimer->Enabled = true;

      // Ensure the event fires before the exit message appears.
      System::Threading::Thread::Sleep((int) interval * 2);
      Console::WriteLine("Press the Enter key to exit the program.");

      // If the timer is declared in a long-running method, use
      // KeepAlive to prevent garbage collection from occurring
      // before the method ends.

   // Handle the Elapsed event.
   static void OnTimedEvent( Object^ /*source*/, ElapsedEventArgs^ /*e*/ )
      Console::WriteLine( "Hello World!" );


int main()
// The example displays the following output:
//       Hello World!
//       Press the Enter key to exit the program.

.NET Framework
Available since 1.1
Return to top