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 보다 큰 값으로 설정되었습니다.

예제

다음 예제에서는 2초마다 이벤트를 발생 Timer.Elapsed 시키는 개체를 인스턴스화 Timer 하고(2000밀리초) 이벤트에 대한 이벤트 처리기를 설정하고 타이머를 시작합니다. 이벤트 처리기는 발생할 때마다 속성의 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

설명

를 로 true 설정하는 Enabled 것은 를 호출하는 Start것과 동일하지만 를 로 설정하는 Enabledfalse 것은 를 호출하는 Stop것과 같습니다.

참고

이벤트를 발생 Elapsed 하도록 하는 신호는 스레드에서 실행하기 위해 항상 큐에 ThreadPool 대기됩니다. 이로 인해 속성이 로 Elapsed 설정된 falseEnabled 이벤트가 발생할 수 있습니다. 메서드의 Stop 코드 예제는 이 경합 상태를 해결하는 한 가지 방법을 보여줍니다.

가 로 true 설정되고 AutoReset 로 설정된 false경우 EnabledTimer 간격이 Elapsed 처음 경과할 때 이벤트를 한 번만 발생합니다.

가 시작된 후 간격이 Timer 설정되면 개수가 다시 설정됩니다. 예를 들어 간격을 5초로 설정한 다음 속성을 true로 설정 Enabled 하면 개수가 설정된 시점에 Enabled 시작됩니다. 개수가 3초일 때 간격을 10초 Elapsed 로 다시 설정하면 이벤트가 로 설정된 후 처음으로 13초 동안 Enabled 발생합니다 true.

참고

Microsoft Visual Studio와 같은 일부 비주얼 디자이너는 새 Timer를 삽입할 때 속성을 로 true 설정합니다Enabled.

적용 대상

추가 정보