Timer.Enabled Proprietà

Definizione

Ottiene o imposta un valore che indica se Timer deve generare l'evento Elapsed.Gets or sets a value indicating whether the Timer should raise the Elapsed event.

public:
 property bool Enabled { bool get(); void set(bool value); };
[System.Timers.TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
[System.Timers.TimersDescription("TimerEnabled")]
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

Valore della proprietà

true se Timer deve generare l'evento Elapsed; in caso contrario, false.true if the Timer should raise the Elapsed event; otherwise, false. Il valore predefinito è false.The default is false.

Attributi

Eccezioni

Non è possibile impostare questa proprietà perché il timer è stato eliminato.This property cannot be set because the timer has been disposed.

La proprietà Interval era impostata su un valore maggiore di MaxValue prima che sia stato abilitato il timer.The Interval property was set to a value greater than MaxValue before the timer was enabled.

Esempi

Nell'esempio seguente viene creata un'istanza di un oggetto Timer che genera l'evento Timer.Elapsed ogni due secondi (2000 millisecondi), imposta un gestore eventi per l'evento e avvia il timer.The following example instantiates a Timer object that fires its Timer.Elapsed event every two seconds (2000 milliseconds), sets up an event handler for the event, and starts the timer. Il gestore eventi Visualizza il valore della proprietà ElapsedEventArgs.SignalTime ogni volta che viene generato.The event handler displays the value of the ElapsedEventArgs.SignalTime property each time it is raised.

using namespace System;
using namespace System::Timers;

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

public:
    static void Demo()
    {
        // Create a timer and set a two second interval.
        aTimer = gcnew System::Timers::Timer();
        aTimer->Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler(Example::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);
    }
};

int main()
{
    Example::Demo();
}
// 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 

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 
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 di Enabled su true equivale alla chiamata Start, mentre l'impostazione di Enabled su false corrisponde alla chiamata di Stop.Setting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.

Nota

Il segnale per generare l'evento Elapsed viene sempre accodato per l'esecuzione in un thread di ThreadPool.The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread. Questo potrebbe comportare la generazione dell'evento Elapsed dopo che la proprietà Enabled è impostata su false.This might result in the Elapsed event being raised after the Enabled property is set to false. Nell'esempio di codice per il metodo Stop viene illustrato un modo per aggirare questo race condition.The code example for the Stop method shows one way to work around this race condition.

Se Enabled è impostato su true e AutoReset è impostato su false, il Timer genera l'evento Elapsed solo una volta, la prima volta che l'intervallo scade.If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.

Se l'intervallo viene impostato dopo l'avvio del Timer, il conteggio viene reimpostato.If the interval is set after the Timer has started, the count is reset. 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 l'intervallo viene reimpostato su 10 secondi quando count è 3 secondi, l'evento Elapsed viene generato per la prima volta 13 secondi dopo l'impostazione di Enabled su true.If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.

Nota

Alcune finestre di progettazione visive, ad esempio quelle in Microsoft Visual Studio, impostano la proprietà Enabled su true quando si inserisce una nuova Timer.Some visual designers, such as those in Microsoft Visual Studio, set the Enabled property to true when inserting a new Timer.

Si applica a

Vedi anche