Timer.Elapsed イベント
アセンブリ: System (system.dll 内)

public: event ElapsedEventHandler^ Elapsed { void add (ElapsedEventHandler^ value); void remove (ElapsedEventHandler^ value); }

Enabled を true に設定し、AutoReset を false に設定した場合、Timer は最初に間隔が経過したときに、1 回だけ Elapsed イベントを発生させます。
Timer が開始した後に Interval を設定すると、カウントがリセットされます。たとえば、間隔を 5 秒に設定し、Enabled を true に設定した場合は、Enabled を設定した時刻からカウントが開始します。カウントが 3 秒のときに間隔を 10 秒にリセットした場合、最初の Elapsed イベントは Enabled を true に設定してから 13 秒後に発生します。
Elapsed イベントは、ThreadPool スレッドで発生します。Elapsed イベントの処理が Interval よりも長びくと、別の ThreadPool スレッドで同じイベントが再発生する場合があります。このため、イベント ハンドラは再入可能であることが必要です。
![]() |
---|
1 つのスレッドでイベント処理メソッドを実行している間に、別のスレッドで Stop メソッドを呼び出したり、Enabled プロパティを false に設定したりする可能性があります。これにより、タイマが停止した後に Elapsed イベントが発生する場合があります。Stop メソッドのコード例では、この競合状態を回避する方法の一例を示しています。 |

Imports System Imports System.Timers Public Class Timer1 Public Shared Sub Main() ' Normally, the timer is declared at the class level, so ' that it doesn't go out of scope when the method ends. ' In this example, the timer is needed only while Main ' is executing. However, KeepAlive must be used at the ' end of Main, to prevent the JIT compiler from allowing ' aggressive garbage collection to occur before Main ' ends. Dim aTimer As New System.Timers.Timer() ' Hook up the Elapsed event for the timer. AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000 aTimer.Enabled = True Console.WriteLine("Press the Enter key to exit the program.") Console.ReadLine() ' Keep the timer alive until the end of Main. GC.KeepAlive(aTimer) End Sub ' Specify what you want to happen when the Elapsed event is ' raised. Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs) Console.WriteLine("Hello World!") End Sub End Class
using System; using System.Timers; public class Timer1 { public static void Main() { // Normally, the timer is declared at the class level, so // that it doesn't go out of scope when the method ends. // In this example, the timer is needed only while Main // is executing. However, KeepAlive must be used at the // end of Main, to prevent the JIT compiler from allowing // aggressive garbage collection to occur before Main // ends. System.Timers.Timer aTimer = new System.Timers.Timer(); // Hook up the Elapsed event for the timer. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); // Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000; aTimer.Enabled = true; Console.WriteLine("Press the Enter key to exit the program."); Console.ReadLine(); // Keep the timer alive until the end of Main. GC.KeepAlive(aTimer); } // Specify what you want to happen when the Elapsed event is // raised. private static void OnTimedEvent(object source, ElapsedEventArgs e) { Console.WriteLine("Hello World!"); } }
#using <system.dll> using namespace System; using namespace System::Timers; public ref class Timer1 { public: static void Demo() { // Normally, the timer is declared at the class level, so // that it doesn't go out of scope when the method ends. // In this example, the timer is needed only while Demo // is executing. However, KeepAlive must be used at the // end of Demo, to prevent the JIT compiler from allowing // aggressive garbage collection to occur before Demo // ends. System::Timers::Timer^ aTimer = gcnew System::Timers::Timer; // Hook up the Elapsed event for the timer. aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::OnTimedEvent ); // Set the Interval to 2 seconds (2000 milliseconds). aTimer->Interval = 2000; aTimer->Enabled = true; Console::WriteLine("Press the Enter key to exit the program."); Console::ReadLine(); // Keep the timer alive until the end of the Demo method. GC::KeepAlive(aTimer); } private: // Specify what you want to happen when the Elapsed event is // raised. static void OnTimedEvent( Object^ /*source*/, ElapsedEventArgs^ /*e*/ ) { Console::WriteLine( "Hello World!" ); } }; int main() { Timer1::Demo(); }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTimer.Elapsed イベントを検索する場合は、下記のリンクをクリックしてください。

- Timer.Elapsed イベントのページへのリンク