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); };
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

属性值

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.SignalTimeThe 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 为与 true 调用相同 Start ,而将设置 Enabled 为与 false 调用相同 StopSetting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.

备注

引发事件的信号 Elapsed 总是排队等候在线程上执行 ThreadPoolThe signal to raise the Elapsed event is always queued for execution on a ThreadPool thread. 这可能导致在 Elapsed Enabled 将属性设置为之后引发事件 falseThis 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 将设置为 false ,则在 Timer Elapsed 第一次时间间隔结束时,只引发一次事件。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 会在设置时开始计数 EnabledFor 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 将设置为第13秒后第一次引发事件 Enabled trueIf 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 TimerSome visual designers, such as those in Microsoft Visual Studio, set the Enabled property to true when inserting a new Timer.

适用于

另请参阅