Timer.AutoReset Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
public:
property bool AutoReset { bool get(); void set(bool value); };
[System.Timers.TimersDescription("TimerAutoReset")]
public bool AutoReset { get; set; }
[<System.Timers.TimersDescription("TimerAutoReset")>]
member this.AutoReset : bool with get, set
Public Property AutoReset As Boolean
Valor de Propriedade
true se o Timer deveria elevar o Elapsed evento cada vez que o intervalo passa; false se o evento Elapsed for elevado apenas uma vez, após a primeira vez que o intervalo decorrer. A predefinição é true.
- Atributos
Exemplos
O exemplo seguinte cria um Timer evento cujo Elapsed evento é ativado após 1,5 segundos. O seu gestor de eventos exibe então "Hello World!" na consola.
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.
open System.Threading
open System.Timers
// Handle the Elapsed event.
let onTimedEvent source e =
printfn "Hello World!"
// Create a timer with a 1.5 second interval.
let interval = 1500.
let aTimer = new Timer(interval)
// Hook up the event handler for the Elapsed event.
aTimer.Elapsed.AddHandler(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.
Thread.Sleep(interval * 2. |> int)
printfn "Press the Enter key to exit the program."
stdin.ReadLine() |> ignore
// 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.
Observações
Se AutoReset for false, o Start método deve ser chamado para que a contagem volte a começar.
Reiniciar o intervalo afeta quando o Elapsed evento é elevado. Por exemplo, se definir o intervalo para 5 segundos e depois definir a Enabled propriedade para true, a contagem começa no momento Enabled em que é definido. Se redefinir o intervalo para 10 segundos quando a contagem é de 3 segundos, o Elapsed evento é elevado pela primeira vez 13 segundos depois de a Enabled propriedade ter sido definida para true.