Timer.Enabled 屬性

定義

取得或設定值,表示 Timer 是否應引發 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

屬性值

如果 Timer 應該引發 Elapsed 事件,則為 true,否則為 falsetrue if the Timer should raise the Elapsed event; otherwise, false. 預設為 falseThe default is false.

屬性

例外狀況

無法設定這個屬性,因為計時器已處置。This property cannot be set because the timer has been disposed.

啟用計時器之前,Interval 屬性已設定為大於 MaxValue 的值。The Interval property was set to a value greater than MaxValue before the timer was enabled.

範例

下列範例會具現Timer化每兩秒Timer.Elapsed引發其事件的物件 (2000 毫秒), 設定事件的事件處理常式, 並啟動計時器。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. 事件處理常式會在每次引發ElapsedEventArgs.SignalTime時顯示內容的值。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 

備註

Enabled Start Stop false將設定為與呼叫相同, 而將設定Enabled為, 則與呼叫相同。 trueSetting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.

注意

引發Elapsed事件的信號一律會排入佇列, 以便ThreadPool線上程上執行。The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread. 這可能會導致Elapsed事件在Enabled屬性設定為false之後引發。This might result in the Elapsed event being raised after the Enabled property is set to false. Stop方法的程式碼範例顯示解決此競爭條件的一種方法。The code example for the Stop method shows one way to work around this race condition.

如果Enabled設定為trueAutoReset設定Elapsedfalse, 則Timer會在第一次到達間隔時引發事件一次。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.

如果在開始後Timer設定間隔, 則會重設計數。If the interval is set after the Timer has started, the count is reset. 例如, 如果您將間隔設為5秒, 然後將Enabled屬性設定為true, 則會在設定的時間Enabled開始計數。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. 如果您在計數為3秒時將間隔重設為10秒Elapsed , 則在設定為true之後Enabled , 第一次有13秒就會引發事件。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.

注意

某些視覺化設計工具 (例如 Microsoft Visual Studio 中的設計工具) Enabled會在true插入新Timer的時, 將屬性設定為。Some visual designers, such as those in Microsoft Visual Studio, set the Enabled property to true when inserting a new Timer.

適用於

另請參閱