Timer.Enabled Timer.Enabled Timer.Enabled Timer.Enabled Property

정의

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이고 그러지 않으면 false입니다.true if the Timer should raise the Elapsed event; otherwise, false. 기본값은 false입니다.The 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 2 초 (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 

설명

설정 Enabledtrue 호출 하는 것 같습니다 Start를 설정 하는 동안 Enabledfalse 호출 하는 것 같습니다 Stop.Setting 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 로 설정 되어 falseTimer 발생 시킵니다는 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, 수 시간에 시작 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 13 초 후 처음으로 이벤트가 발생 Enabled 로 설정 된 true합니다.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.

적용 대상

추가 정보