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; }
[System.Timers.TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
[<System.Timers.TimersDescription("TimerEnabled")>]
member this.Enabled : bool with get, set
[<System.Timers.TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")>]
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

Wartość właściwości

Boolean

trueTimer jeśli element 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
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 Enabled wartości jest true takie samo jak wywołanie Startmetody , podczas gdy ustawienie Enabled false ma wartość , jest 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 wywołanie zdarzenia po Enabled ustawieniu właściwości na falsewartość . Przykładowy kod metody Stop przedstawia jeden ze sposobów obejścia tego warunku wyścigu.

Jeśli Enabled parametr jest ustawiony na i AutoReset jest ustawiony true na falsewartość , Timer parametr zgłasza Elapsed zdarzenie tylko raz, gdy interwał upłynie po raz pierwszy.

Jeśli interwał zostanie ustawiony po uruchomieniu 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 jest zgłaszane po raz pierwszy 13 sekund po Enabled ustawieniu wartości true.

Uwaga

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

Dotyczy

Zobacz też