共用方式為


Timer.Enabled 屬性

定義

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

屬性值

如果 Timer 應該引發 Elapsed 事件,則為 true,否則為 false。 預設為 false

屬性

例外狀況

無法設定這個屬性,因為計時器已處置。

屬性 Interval 在啟用計時器之前設定為大於 Int32.MaxValue 的值。

範例

下列範例會具現化 Timer 物件,每隔兩秒 (2000 毫秒) 引發其 Timer.Elapsed 事件、設定事件的事件處理常式,然後啟動計時器。 事件處理常式會在每次引發時顯示 屬性的值 ElapsedEventArgs.SignalTime

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

備註

將 設定 Enabled 為 與呼叫 Start 相同,而 將 設定 Enabledfalse 為 與呼叫 Stop 相同。 true

注意

引發 Elapsed 事件的訊號一律會排入佇列,以線上程上 ThreadPool 執行。 這可能會導致 Elapsed 屬性設定為 false 之後 Enabled 引發 事件。 方法的程式 Stop 代碼範例示範一種解決此競爭狀況的方式。

如果 Enabled 設定為 trueAutoReset 設定為 false ,則 Timer 只會引發 Elapsed 事件一次,第一次經過間隔。

如果在 開始之後 Timer 設定間隔,則會重設計數。 例如,如果您將間隔設定為 5 秒,然後將 屬性設定 Enabledtrue ,則計數會在設定時 Enabled 開始。 如果您在計數為 3 秒時將間隔重設為 10 秒,則會 Elapsed 在 設定為 之後 Enabledtrue 的第一次 13 秒引發事件。

注意

某些視覺化設計工具,例如 Microsoft Visual Studio 中的設計工具,會在插入新的 Timer 時,將 Enabled 屬性設定為 true

適用於

另請參閱