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 、2秒 ( 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.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 

注釈

Enabled IntervalプロパティElapsedtrueで、プロパティによって定義された時間間隔 (ミリ秒単位) が経過すると、イベントが発生します。The Elapsed event is raised if the Enabled property is true and the time interval (in milliseconds) defined by the Interval property elapses. プロパティがtrueの場合、イベントはIntervalプロパティによって定義された間隔で繰り返し発生します。それ以外の場合は、最初にInterval値が経過したときに1回だけイベントが発生します。 AutoResetIf 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秒に設定し、をにEnabled true設定した場合、 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. [カウント] Elapsedを3秒に設定した場合、間隔を10秒にリセットすると、がにtrue設定さEnabledれてから13秒後にイベントが発生します。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.

プロパティがnullの場合、 ElapsedイベントはThreadPoolスレッドで発生します。 SynchronizingObjectIf 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ときに、1つのスレッドで実行されることがあります。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メソッドは、この競合状態を回避するために 1 つのメソッドを示しています。The example code for the Stop method shows one way to avoid this race condition.

がでSynchronizingObject Dispose Stop Enabled falseないnull場合でも、メソッドまたはメソッドが呼び出された後、またはプロパティがに設定された後に、イベントが発生する可能性があります。これは、Elapsed 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. この競合状態がのイベント ハンドラーを示すフラグを設定するには解決するのには 1 つのメソッド、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は、イベント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.

適用対象

こちらもご覧ください