Timer.Elapsed 이벤트

정의

간격이 경과하면 발생합니다.

public:
 event System::Timers::ElapsedEventHandler ^ Elapsed;
public event System.Timers.ElapsedEventHandler Elapsed;
[System.Timers.TimersDescription("TimerIntervalElapsed")]
public event System.Timers.ElapsedEventHandler Elapsed;
member this.Elapsed : System.Timers.ElapsedEventHandler 
[<System.Timers.TimersDescription("TimerIntervalElapsed")>]
member this.Elapsed : System.Timers.ElapsedEventHandler 
Public Custom Event Elapsed As ElapsedEventHandler 

이벤트 유형

특성

예제

다음 예제에서는 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

설명

Elapsed 속성이 이고 속성 true 이 정의 Interval 한 시간 간격(밀리초)이 경과하면 Enabled 이벤트가 발생합니다. 속성이 AutoResettrue이면 속성이 정의 Interval 한 간격으로 이벤트가 반복적으로 발생합니다. 그렇지 않으면 값이 처음 경과할 때 Interval 이벤트가 한 번만 발생합니다.

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

속성이 SynchronizingObjectElapsed 면 이벤트가 스레드에서 ThreadPool 발생합니다.null 이벤트 처리가 Elapsed 보다 Interval오래 지속되는 경우 다른 ThreadPool 스레드에서 이벤트가 다시 발생할 수 있습니다. 이 경우 이벤트 처리기를 다시 입력해야 합니다.

참고

이벤트 처리 메서드는 다른 스레드가 메서드를 호출 Stop 하거나 속성을 false로 설정하는 Enabled 동시에 한 스레드에서 실행될 수 있습니다. 이로 인해 타이머가 Elapsed 중지된 후 이벤트가 발생할 수 있습니다. 메서드의 Stop 예제 코드는 이 경합 상태를 방지하는 한 가지 방법을 보여줍니다.

가 이 아니 Elapsednull더라도 SynchronizingObject 또는 메서드가 호출된 후 Dispose 또는 Stop 속성이 로 설정된 false후에 Enabled 이벤트가 발생할 수 있습니다. 이벤트를 발생 Elapsed 시키지 않는 신호는 스레드 풀 스레드에서 실행하기 위해 항상 큐에 대기되기 때문입니다. 이 경합 상태를 resolve 한 가지 방법은 이벤트 처리기에 후속 이벤트를 무시하도록 지시하는 플래그를 Elapsed 설정하는 것입니다.

Timer 구성 요소는 이벤트에 대한 Elapsed 이벤트 처리기에서 throw된 모든 예외를 catch하고 표시하지 않습니다. 이 동작은 향후 .NET Framework 릴리스에서 변경될 수 있습니다.

적용 대상

추가 정보