Timer.Enabled 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 Enabled { bool get(); void set(bool value); };
[System.Timers.TimersDescription("TimerEnabled")]
public bool Enabled { get; set; }
[<System.Timers.TimersDescription("TimerEnabled")>]
member this.Enabled : bool with get, set
Public Property Enabled As Boolean
Valor de Propriedade
true se o Timer elevar o Elapsed evento; caso contrário, false. A predefinição é false.
- Atributos
Exceções
Esta propriedade não pode ser definida porque o temporizador foi eliminado.
A Interval propriedade foi definida para um valor superior ao Int32.MaxValue antes de o temporizador ser ativado.
Exemplos
O exemplo seguinte instancia um Timer objeto que dispara o seu Timer.Elapsed evento a cada dois segundos (2000 milissegundos), cria um manipulador de eventos para o evento e inicia o temporizador. O gestor de eventos mostra o valor da ElapsedEventArgs.SignalTime propriedade cada vez que esta é elevada.
using System;
using System.Timers;
public class Example
{
private static Timer aTimer;
public static void Main()
{
// Create a timer and set a two second interval.
aTimer = new System.Timers.Timer();
aTimer.Interval = 2000;
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset = true;
// Start the timer
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program at any time... ");
Console.ReadLine();
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
// The example displays output like the following:
// Press the Enter key to exit the program at any time...
// The Elapsed event was raised at 5/20/2015 8:48:58 PM
// The Elapsed event was raised at 5/20/2015 8:49:00 PM
// The Elapsed event was raised at 5/20/2015 8:49:02 PM
// The Elapsed event was raised at 5/20/2015 8:49:04 PM
// The Elapsed event was raised at 5/20/2015 8:49:06 PM
open System.Timers
let onTimedEvent source (e: ElapsedEventArgs) =
printfn $"The Elapsed event was raised at {e.SignalTime}"
// Create a timer and set a two second interval.
let aTimer = new Timer()
aTimer.Interval <- 2000
// Hook up the Elapsed event for the timer.
aTimer.Elapsed.AddHandler onTimedEvent
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset <- true
// Start the timer
aTimer.Enabled <- true
printfn "Press the Enter key to exit the program at any time... "
stdin.ReadLine() |> ignore
// The example displays output like the following:
// Press the Enter key to exit the program at any time...
// The Elapsed event was raised at 5/20/2015 8:48:58 PM
// The Elapsed event was raised at 5/20/2015 8:49:00 PM
// The Elapsed event was raised at 5/20/2015 8:49:02 PM
// The Elapsed event was raised at 5/20/2015 8:49:04 PM
// The Elapsed event was raised at 5/20/2015 8:49:06 PM
Imports System.Timers
Public Module Example
Private aTimer As Timer
Public Sub Main()
' Create a timer and set a two second interval.
aTimer = New System.Timers.Timer()
aTimer.Interval = 2000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Have the timer fire repeated events (true is the default)
aTimer.AutoReset = True
' Start the timer
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program at any time... ")
Console.ReadLine()
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
End Sub
End Module
' The example displays output like the following:
' Press the Enter key to exit the program at any time...
' The Elapsed event was raised at 5/20/2015 8:48:58 PM
' The Elapsed event was raised at 5/20/2015 8:49:00 PM
' The Elapsed event was raised at 5/20/2015 8:49:02 PM
' The Elapsed event was raised at 5/20/2015 8:49:04 PM
' The Elapsed event was raised at 5/20/2015 8:49:06 PM
Observações
Definir Enabled para true é o mesmo que chamar Start, enquanto definir Enabled para false é o mesmo que chamar Stop.
Note
O sinal para levantar o Elapsed evento está sempre na fila para execução num ThreadPool thread. Isto pode resultar em que o Elapsed evento seja elevado depois de a Enabled propriedade estar definida para false. O exemplo de código para o Stop método mostra uma forma de contornar esta condição de corrida.
Se Enabled for definido como true e AutoReset for definido como false, o Timer evento aumenta Elapsed apenas uma vez, na primeira vez que o intervalo decorre.
Se o intervalo for definido depois de ter Timer começado, a contagem é reiniciada. 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 reiniciar o intervalo para 10 segundos quando a contagem é de 3 segundos, o Elapsed evento é elevado pela primeira vez 13 segundos depois Enabled de ter sido definido para true.
Note
Alguns designers visuais, como os do Microsoft Visual Studio, definem a propriedade Enabled para true ao inserir um novo Timer.