Timer.Elapsed Timer.Elapsed Timer.Elapsed Timer.Elapsed Event

정의

간격이 경과하면 발생합니다.Occurs when the interval elapses.

public:
 event System::Timers::ElapsedEventHandler ^ Elapsed;
[System.Timers.TimersDescription("Occurs when the Interval has elapsed.")]
[System.Timers.TimersDescription("TimerIntervalElapsed")]
public event System.Timers.ElapsedEventHandler Elapsed;
member this.Elapsed : System.Timers.ElapsedEventHandler 
Public Custom Event Elapsed As ElapsedEventHandler 
특성

예제

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

설명

Elapsed 이벤트는 합니다 Enabled 속성은 true 정의한 시간 간격 (밀리초)는 Interval 속성 경과 합니다.The Elapsed event is raised if the Enabled property is true and the time interval (in milliseconds) defined by the Interval property elapses. 경우는 AutoReset 속성은 true, 정의한 간격으로 이벤트를 반복적으로 발생 합니다 Interval 속성 이벤트 발생이 고, 그렇지 처음으로 한 번만 Interval 경과 값.If the AutoReset property is true, the event is raised repeatedly at an interval defined by the Interval property; otherwise, the event is raised only once, the first time the Interval value elapses.

하는 경우 Interval 한 후 설정 됩니다는 Timer 에 시작 수 다시 설정 됩니다.If Interval is set after the Timer has started, the count is reset. 예를 들어 간격을 5 초로 설정 하 고 설정한 Enabledtrue를 수 시간에 시작 Enabled 설정 됩니다.For example, if you set the interval to 5 seconds and then set Enabled 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.

경우는 SynchronizingObject 속성은 nullElapsed 에서 이벤트가 발생을 ThreadPool 스레드입니다.If the SynchronizingObject property is null,the Elapsed event is raised on a ThreadPool thread. 경우 처리를 Elapsed 보다 오래 지속 되는 이벤트 Interval, 다른 이벤트를 다시 발생할 수 있습니다 ThreadPool 스레드입니다.If the processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. 이 경우 이벤트 처리기는 재진입 있어야 합니다.In this situation, the event handler should be reentrant.

참고

이벤트 처리 메서드는 다른 스레드를 호출 하는 동시에 한 스레드에서 실행 될 수 있습니다 합니다 Stop 가져오거나 메서드를 Enabled 속성을 false입니다.The event-handling method might run on one thread at the same time that another thread calls the Stop method or sets the Enabled property to false. 이 발생할 수는 Elapsed 타이머가 중지 된 후 발생 하는 이벤트입니다.This might result in the Elapsed event being raised after the timer is stopped. 에 대 한 예제 코드는 Stop 메서드가 경합을 방지 하는 방법을 보여 줍니다.The example code for the Stop method shows one way to avoid this race condition.

경우에 SynchronizingObject 아닙니다 null, Elapsed 이벤트 후 발생할 수 있습니다를 Dispose 또는 Stop 메서드를 호출한 후 나 합니다 Enabled 속성이로 설정 된 false이므로 를발생시키는신호Elapsed 스레드 풀 스레드에서 실행 이벤트는 항상 대기 합니다.Even if SynchronizingObject is not null, Elapsed events can occur after the Dispose or Stop method has been called or after the Enabled property has been set to false, because the signal to raise the Elapsed event is always queued for execution on a thread pool thread. 이 경합 상태에 대 한 이벤트 처리기를 지시 하는 플래그를 설정 하는 것을 해결할 수는 Elapsed 후속 이벤트를 무시 하는 이벤트입니다.One way to resolve this race condition is to set a flag that tells the event handler for the Elapsed event to ignore subsequent events.

합니다 Timer 구성 요소를 catch 하 고에 대 한 이벤트 처리기에서 throw 된 모든 예외를 표시 하지 않습니다는 Elapsed 이벤트입니다.The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. 이 동작은.NET Framework의 이후 릴리스에서 변경 될 수 있습니다.This behavior is subject to change in future releases of the .NET Framework.

적용 대상

추가 정보