Timer.Enabled Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
public:
property bool Enabled { bool get(); void set(bool value); };
public bool Enabled { get; set; }
[System.Timers.TimersDescription("TimerEnabled")]
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
[<System.Timers.TimersDescription("TimerEnabled")>]
member this.Enabled : bool with get, set
Public Property Enabled As Boolean
Valore della proprietà
true se deve Timer generare l'evento Elapsed ; in caso contrario, false. Il valore predefinito è false.
- Attributi
Eccezioni
Impossibile impostare questa proprietà perché il timer è stato eliminato.
La Interval proprietà è stata impostata su un valore maggiore di Int32.MaxValue prima dell'abilitazione del timer.
Esempio
Nell'esempio seguente viene creata un'istanza di un Timer oggetto che genera l'evento Timer.Elapsed ogni due secondi (2000 millisecondi), configura un gestore eventi per l'evento e avvia il timer. Il gestore eventi visualizza il valore della ElapsedEventArgs.SignalTime proprietà ogni volta che viene generato.
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
Commenti
L'impostazione su Enabledtrue equivale a chiamare Start, mentre l'impostazione Enabled su false equivale a chiamare Stop.
Annotazioni
Il segnale per generare l'evento viene sempre accodato per l'esecuzione Elapsed in un ThreadPool thread. Questo potrebbe comportare la generazione dell'evento Elapsed dopo che la Enabled proprietà è impostata su false. L'esempio di codice per il Stop metodo mostra un modo per aggirare questa race condition.
Se Enabled è impostato su e AutoReset è impostato true su false, Timer genera l'evento Elapsed una sola volta, la prima volta che l'intervallo viene trascorso.
Se l'intervallo viene impostato dopo l'avvio Timer di , il conteggio viene reimpostato. Ad esempio, se si imposta l'intervallo su 5 secondi e quindi si imposta la Enabled proprietà su true, il conteggio inizia al momento Enabled dell'impostazione. Se si reimposta l'intervallo su 10 secondi quando il conteggio è di 3 secondi, l'evento viene generato per la prima volta 13 secondi dopo Enabled l'impostazione Elapsed su true.
Annotazioni
Alcune finestre di progettazione visiva, ad esempio quelle in Microsoft Visual Studio, impostano la Enabled proprietà su true quando si inserisce un nuovo Timeroggetto .