Timer.AutoReset Proprietà

Definizione

Ottiene o imposta un valore booleano che indica se Timer deve generare l'evento Elapsed una sola volta (false) o più volte (true).Gets or sets a Boolean indicating whether the Timer should raise the Elapsed event only once (false) or repeatedly (true).

public:
 property bool AutoReset { bool get(); void set(bool value); };
[System.Timers.TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
[System.Timers.TimersDescription("TimerAutoReset")]
public bool AutoReset { get; set; }
member this.AutoReset : bool with get, set
Public Property AutoReset As Boolean

Valore della proprietà

true se Timer deve generare l'evento Elapsed allo scadere di ogni intervallo; false se deve generare l'evento Elapsed solo una volta, in corrispondenza della prima scadenza dell'intervallo.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. Il valore predefinito è true.The default is true.

Attributi

Esempi

Nell'esempio seguente viene creata una Timer il cui Elapsed evento viene attivato dopo 1,5 secondi.The following example creates a Timer whose Elapsed event fires after 1.5 seconds. Il gestore eventi Visualizza quindi "Hello World!"Its event handler then displays "Hello World!" nella console di.on the console.

#using <system.dll>

using namespace System;
using namespace System::Timers;

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

public:
   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.");
      Console::ReadLine();

      // If the timer is declared in a long-running method, use
      // KeepAlive to prevent garbage collection from occurring
      // before the method ends.
      //GC::KeepAlive(aTimer);
   }

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

};

int main()
{
   Timer2::Main();
}
// The example displays the following output:
//       Hello World!
//       Press the Enter key to exit the program.
using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer with a 1.5 second interval.
        double interval = 1500.0;
        aTimer = new System.Timers.Timer(interval);
        
        // Hook up the event handler for the Elapsed event.
        aTimer.Elapsed += new 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.");
        Console.ReadLine();
    }
 
    // Handle the Elapsed event.
    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    {
        Console.WriteLine("Hello World!");
    }
}
// This example displays the following output:
//       Hello World!
//       Press the Enter key to exit the program.
Imports System.Timers

Public Module Example
    Private aTimer As System.Timers.Timer

    Public Sub Main()
        ' Create a timer with a 1.5 second interval.
        Dim interval As Double = 1500.0
        aTimer = New System.Timers.Timer(interval)

        ' Hook up the event handler for the Elapsed event.
        AddHandler aTimer.Elapsed, AddressOf 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(CInt(interval * 2))
        Console.WriteLine("Press the Enter key to exit the program.")
        Console.ReadLine()

        ' If the timer is declared in a long-running method, use
        ' KeepAlive to prevent garbage collection from occurring
        ' before the method ends.
        'GC.KeepAlive(aTimer)
    End Sub

    ' Specify what you want to happen when the Elapsed event is 
    ' raised.
    Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Module
' This example displays the following output:
'       Hello World!
'       Press the Enter key to exit the program.

Commenti

Se il Timer è già abilitato quando viene chiamato il metodo Start, l'intervallo viene reimpostato.If the Timer is already enabled when the Start method is called, the interval is reset. Se AutoReset è false, è necessario chiamare il metodo Start per riavviarlo.If AutoReset is false, the Start method must be called in order to start the count again.

La reimpostazione dell'intervallo influiscono sul momento in cui viene generato l'evento Elapsed.Resetting the interval affects when the Elapsed event is raised. Se, ad esempio, si imposta l'intervallo su 5 secondi e quindi si imposta la proprietà Enabled su true, il conteggio inizia al momento in cui Enabled è impostato.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. Se si reimposta l'intervallo su 10 secondi quando il conteggio è di 3 secondi, l'evento Elapsed viene generato per la prima volta 13 secondi dopo che la proprietà Enabled è stata impostata su true.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.

Si applica a

Vedi anche