Timer.Elapsed イベント

定義

間隔が経過すると発生します。Occurs when the interval elapses.

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

イベントの種類

ElapsedEventHandler
属性

次の例では、 Timer 2 秒 (2000 ミリ秒) ごとにイベントを発生させるオブジェクトをインスタンス化し、イベント Timer.Elapsed のイベントハンドラーを設定して、タイマーを開始します。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 ます。それ以外の場合は、最初に値が経過したときに1回だけイベントが発生し 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秒に設定し、をに設定した場合、 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. [カウント] を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 またはプロパティをに設定するときに、1つのスレッドで実行されることがあり 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メソッドは、この競合状態を回避するために 1 つのメソッドを示しています。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. この競合状態がのイベント ハンドラーを示すフラグを設定するには解決するのには 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 .NET Framework.

適用対象

こちらもご覧ください