Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Timer.AutoReset Property

Gets or sets a value indicating whether the Timer should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses.

[Visual Basic]
Public Property AutoReset As Boolean
[C#]
public bool AutoReset {get; set;}
[C++]
public: __property bool get_AutoReset();
public: __property void set_AutoReset(bool);
[JScript]
public function get AutoReset() : Boolean;
public function set AutoReset(Boolean);

Property Value

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.

Remarks

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.

Example

[Visual Basic, C#, C++] The following example creates a Timer that displays "Hello World!" on the console after ten seconds.

[Visual Basic, C#, C++] Use the System.Timers namespace for this example.

[Visual Basic] 
Public Class Timer2
    
    Public Shared Sub Main()
        ' Create a new Timer with Interval set to 10 seconds.
        Dim aTimer As New System.Timers.Timer(10000)
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        ' Only raise the event the first time Interval elapses.
        aTimer.AutoReset = False
        aTimer.Enabled = True
        
        Console.WriteLine("Press 'q' to quit the sample.")
        While Console.Read() <> CInt("q")
        End While
    End Sub

    ' Specify what you want to happen when the event is raised.
    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Class


[C#] 
public class Timer2
 {
 
     public static void Main()
     {
       // Create a new Timer with Interval set to 10 seconds.
       System.Timers.Timer aTimer = new System.Timers.Timer(10000);
       aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
       // Only raise the event the first time Interval elapses.
       aTimer.AutoReset = false;
       aTimer.Enabled = true;
 
       Console.WriteLine("Press \'q\' to quit the sample.");
       while(Console.Read()!='q');
     }
 
     // Specify what you want to happen when the event is raised.
     private static void OnTimedEvent(object source, ElapsedEventArgs e) 
     {
       Console.WriteLine("Hello World!");
     }
 }
 

[C++] 
public __gc class Timer2
{
public:
   static void Main() {
      // Create a new Timer with Interval set to 10 seconds.
      System::Timers::Timer* aTimer = new System::Timers::Timer(10000);
      aTimer->Elapsed+=new ElapsedEventHandler(0, OnTimedEvent);
      // Only raise the event the first time Interval elapses.
      aTimer->AutoReset = false;
      aTimer->Enabled = true;
   }
private:

   // Specify what you want to happen when the Elapsed event is raised.
   static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
   {
      Console::WriteLine(S"Hello World!");
   }
};

int main()
{
   Timer2::Main();

   Console::WriteLine(S"Press \'q\' to quit the sample.");
   while(Console::Read()!='q');
} 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Timer Class | Timer Members | System.Timers Namespace | Interval | Enabled | Start | Elapsed

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