言語

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 

イベントの種類

属性

例

次の例では、Timer イベントを 2 秒 (2000 ミリ秒) ごとに発生するTimer.Elapsed オブジェクトをインスタンス化し、イベントのイベント ハンドラーを設定し、タイマーを開始します。 イベント ハンドラーは、発生するたびに、 ElapsedEventArgs.SignalTime プロパティの値を表示します。

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 イベントは、Enabled プロパティがtrueされ、Interval プロパティによって定義された時間間隔 (ミリ秒) が経過した場合に発生します。 AutoReset プロパティがtrue場合、Interval プロパティによって定義された間隔でイベントが繰り返し発生します。それ以外の場合、イベントは 1 回だけ発生し、Interval値が初めて経過した場合に発生します。

Intervalの開始後にTimerが設定されている場合、カウントはリセットされます。 たとえば、間隔を 5 秒に設定し、 Enabled を true に設定すると、カウントは Enabled が設定された時点で開始されます。 カウントが 3 秒のときに間隔を 10 秒にリセットすると、 Elapsed イベントは、 Enabled が true に設定されてから 13 秒間初めて発生します。

SynchronizingObject プロパティがnull場合、Elapsed イベントはThreadPool スレッドで発生します。 Elapsed イベントの処理がIntervalよりも長く続く場合は、別のThreadPool スレッドでイベントが再度発生する可能性があります。 この状況では、イベント ハンドラーを再入する必要があります。

Note

イベント処理メソッドは、別のスレッドが Stop メソッドを呼び出すか、 Enabled プロパティを false に設定すると同時に、1 つのスレッドで実行される場合があります。 これにより、タイマーの停止後に Elapsed イベントが発生する可能性があります。 Stop メソッドのコード例は、この競合状態を回避する 1 つの方法を示しています。

SynchronizingObjectがnullされていない場合でも、Elapsedイベントは、DisposeメソッドまたはStop メソッドが呼び出された後、またはEnabled プロパティがfalseに設定された後に発生する可能性があります。これは、Elapsed イベントを発生させるシグナルがスレッド プール スレッドで実行するために常にキューに入れられます。 この競合状態を解決する 1 つの方法は、後続のイベントを無視するように Elapsed イベントのイベント ハンドラーに指示するフラグを設定することです。

Timer コンポーネントは、Elapsed イベントのイベント ハンドラーによってスローされるすべての例外をキャッチして抑制します。 この動作は、.NET Framework の今後のリリースで変更される可能性があります。

適用対象

こちらもご覧ください