Timer.Enabled Właściwość

Definicja

Pobiera lub ustawia wartość wskazującą, czy Timer zdarzenie powinno zostać wywołane Elapsed .

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

Wartość właściwości

true jeśli element Timer powinien zgłosić Elapsed zdarzenie; w przeciwnym razie false. Wartość domyślna to false.

Atrybuty

Wyjątki

Nie można ustawić tej właściwości, ponieważ czasomierz został usunięty.

Właściwość Interval została ustawiona na wartość większą niż Int32.MaxValue przed włączeniem czasomierza.

Przykłady

Poniższy przykład tworzy wystąpienie Timer obiektu, który uruchamia zdarzenie Timer.Elapsed co dwie sekundy (2000 milisekund), konfiguruje procedurę obsługi zdarzeń dla zdarzenia i uruchamia czasomierz. Program obsługi zdarzeń wyświetla wartość ElapsedEventArgs.SignalTime właściwości za każdym razem, gdy jest wywoływana.

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
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

Uwagi

Ustawienie wartości trueEnabled jest takie samo jak wywołanie Startmetody , podczas gdy ustawienie jest Enabledfalse takie samo jak wywołanie Stopmetody .

Uwaga

Sygnał do podniesienia Elapsed zdarzenia jest zawsze w kolejce do wykonania w wątku ThreadPool . Może to spowodować Elapsed podniesienie zdarzenia po Enabled ustawieniu właściwości na falsewartość . Przykładowy kod Stop dla metody przedstawia jeden ze sposobów obejścia tego warunku wyścigu.

Jeśli Enabled ustawiono wartość true i AutoReset jest ustawiona na falsewartość , parametr Timer zgłasza Elapsed zdarzenie tylko raz, po raz pierwszy upłynął interwał.

Jeśli interwał zostanie ustawiony po rozpoczęciu Timer , liczba zostanie zresetowana. Jeśli na przykład ustawisz interwał na 5 sekund, a następnie ustawisz Enabled właściwość na truewartość , liczba rozpoczyna się w czasie Enabled jest ustawiona. Jeśli zresetujesz interwał do 10 sekund, gdy licznik wynosi 3 sekundy, Elapsed zdarzenie zostanie podniesione po raz pierwszy 13 sekund po Enabled ustawieniu wartości true.

Uwaga

Niektórzy projektanci wizualizacji, tacy jak ci w programie Microsoft Visual Studio, ustawiają Enabled właściwość na wartość true podczas wstawiania nowego Timerelementu .

Dotyczy

Zobacz też