Timer イベント

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。 ( Component から継承されます。) |
![]() | Elapsed | 間隔が経過すると発生します。 |

Timer イベント

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。 ( Component から継承されます。) |
![]() | Tick | 指定したタイマの間隔が経過し、タイマが有効である場合に発生します。 |

Timer クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(True)> _ Public NotInheritable Class Timer Inherits MarshalByRefObject Implements IDisposable

![]() |
---|
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
TimerCallback デリゲートを使用して、Timer で実行するメソッドを指定します。タイマ デリゲートは、タイマの構築時に指定され、変更することはできません。このメソッドは、タイマを作成したスレッドで実行されず、システムが提供する ThreadPool スレッドで実行されます。
タイマを作成するときは、メソッドを最初に実行するまでの待機時間 (期限) と、その後の実行ごとに待機する時間 (期間) を指定できます。Change メソッドを使用して、これらの値を変更したり、タイマを無効にしたりできます。
![]() |
---|
Timer を使用している間は、このクラスへの参照を保持しておく必要があります。他のマネージ オブジェクトと同様、まったく参照されていない場合、Timer はガベージ コレクションの対象となります。Timer がアクティブであっても、ガベージ コレクションの対象から除外されることはありません。 |
タイマが不要になった場合は、Dispose メソッドを使用して、そのタイマが保持しているリソースを解放します。タイマが破棄されたときにシグナルを受信するには、WaitHandle を受け取る Dispose(WaitHandle) メソッド オーバーロードを使用します。WaitHandle は、タイマが破棄されると通知されます。
タイマによって実行されるコールバック メソッドは、ThreadPool スレッドで呼び出されるため、再入可能である必要があります。タイマ間隔がコールバックの実行に必要な時間よりも小さい場合、または、スレッド プール スレッドがすべて使用中でコールバックが複数回キューに置かれる場合は、コールバックを 2 つのスレッド プール スレッドで同時に実行できます。

Imports Microsoft.VisualBasic Imports System Imports System.Threading Public Class TimerExample <MTAThread> _ Shared Sub Main() Dim autoEvent As New AutoResetEvent(False) Dim statusChecker As New StatusChecker(10) ' Create the delegate that invokes methods for the timer. Dim timerDelegate As TimerCallback = _ AddressOf statusChecker.CheckStatus ' Create a timer that signals the delegate to invoke ' CheckStatus after one second, and every 1/4 second ' thereafter. Console.WriteLine("{0} Creating timer." & vbCrLf, _ DateTime.Now.ToString("h:mm:ss.fff")) Dim stateTimer As Timer = _ New Timer(timerDelegate, autoEvent, 1000, 250) ' When autoEvent signals, change the period to every ' 1/2 second. autoEvent.WaitOne(5000, False) stateTimer.Change(0, 500) Console.WriteLine(vbCrLf & "Changing period." & vbCrLf) ' When autoEvent signals the second time, dispose of ' the timer. autoEvent.WaitOne(5000, False) stateTimer.Dispose() Console.WriteLine(vbCrLf & "Destroying timer.") End Sub End Class Public Class StatusChecker Dim invokeCount, maxCount As Integer Sub New(count As Integer) invokeCount = 0 maxCount = count End Sub ' This method is called by the timer delegate. Sub CheckStatus(stateInfo As Object) Dim autoEvent As AutoResetEvent = _ DirectCast(stateInfo, AutoResetEvent) invokeCount += 1 Console.WriteLine("{0} Checking status {1,2}.", _ DateTime.Now.ToString("h:mm:ss.fff"), _ invokeCount.ToString()) If invokeCount = maxCount Then ' Reset the counter and signal to stop the timer. invokeCount = 0 autoEvent.Set() End If End Sub End Class
using System; using System.Threading; class TimerExample { static void Main() { AutoResetEvent autoEvent = new AutoResetEvent(false); StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus); // Create a timer that signals the delegate to invoke // CheckStatus after one second, and every 1/4 second // thereafter. Console.WriteLine("{0} Creating timer.\n", DateTime.Now.ToString("h:mm:ss.fff")); Timer stateTimer = new Timer(timerDelegate, autoEvent, 1000, 250); // When autoEvent signals, change the period to every // 1/2 second. autoEvent.WaitOne(5000, false); stateTimer.Change(0, 500); Console.WriteLine("\nChanging period.\n"); // When autoEvent signals the second time, dispose of // the timer. autoEvent.WaitOne(5000, false); stateTimer.Dispose(); Console.WriteLine("\nDestroying timer."); } } class StatusChecker { int invokeCount, maxCount; public StatusChecker(int count) { invokeCount = 0; maxCount = count; } // This method is called by the timer delegate. public void CheckStatus(Object stateInfo) { AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; Console.WriteLine("{0} Checking status {1,2}.", DateTime.Now.ToString("h:mm:ss.fff"), (++invokeCount).ToString()); if(invokeCount == maxCount) { // Reset the counter and signal Main. invokeCount = 0; autoEvent.Set(); } } }
using namespace System; using namespace System::Threading; ref class StatusChecker { private: int invokeCount; int maxCount; public: StatusChecker( int count ) : invokeCount( 0 ), maxCount( count ) {} // This method is called by the timer delegate. void CheckStatus( Object^ stateInfo ) { AutoResetEvent^ autoEvent = dynamic_cast<AutoResetEvent^>(stateInfo); Console::WriteLine( "{0} Checking status {1,2}.", DateTime::Now.ToString( "h:mm:ss.fff" ), (++invokeCount).ToString() ); if ( invokeCount == maxCount ) { // Reset the counter and signal main. invokeCount = 0; autoEvent->Set(); } } }; int main() { AutoResetEvent^ autoEvent = gcnew AutoResetEvent( false ); StatusChecker^ statusChecker = gcnew StatusChecker( 10 ); // Create the delegate that invokes methods for the timer. TimerCallback^ timerDelegate = gcnew TimerCallback( statusChecker, &StatusChecker::CheckStatus ); // Create a timer that signals the delegate to invoke CheckStatus // after one second, and every 1/4 second thereafter. Console::WriteLine( "{0} Creating timer.\n", DateTime::Now.ToString( "h:mm:ss.fff" ) ); Timer^ stateTimer = gcnew Timer( timerDelegate,autoEvent,1000,250 ); // When autoEvent signals, change the period to every 1/2 second. autoEvent->WaitOne( 5000, false ); stateTimer->Change( 0, 500 ); Console::WriteLine( "\nChanging period.\n" ); // When autoEvent signals the second time, dispose of the timer. autoEvent->WaitOne( 5000, false ); stateTimer->~Timer(); Console::WriteLine( "\nDestroying timer." ); }
import System.*; import System.Threading.*; import System.Threading.Thread; class TimerExample { public static void main(String[] args) { AutoResetEvent autoEvent = new AutoResetEvent(false); StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback( statusChecker.CheckStatus); // Create a timer that signals the delegate to invoke // CheckStatus after one second, and every 1/4 second // thereafter. Console.WriteLine("{0} Creating timer.\n", System.DateTime.get_Now().ToString("h:mm:ss.fff")); Timer stateTimer = new Timer(timerDelegate, autoEvent, 1000, 250); // When autoEvent signals, change the period to every // 1/2 second. autoEvent.WaitOne(5000, false); stateTimer.Change(0, 500); Console.WriteLine("\nChanging period.\n"); // When autoEvent signals the second time, dispose of // the timer. autoEvent.WaitOne(5000, false); stateTimer.Dispose(); Console.WriteLine("\nDestroying timer."); } //main } //TimerExample class StatusChecker { private int invokeCount, maxCount; public StatusChecker(int count) { invokeCount = 0; maxCount = count; } //StatusChecker // This method is called by the timer delegate. public void CheckStatus(Object stateInfo) { AutoResetEvent autoEvent = ((AutoResetEvent)(stateInfo)); Console.WriteLine("{0} Checking status {1,2}.", System.DateTime.get_Now().ToString("h:mm:ss.fff"), String.valueOf(++invokeCount)); if (invokeCount == maxCount) { // Reset the counter and signal Main. invokeCount = 0; autoEvent.Set(); } } //CheckStatus } //StatusChecker

System.MarshalByRefObject
System.Threading.Timer


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


Timer クラス
アセンブリ: System (system.dll 内)


![]() |
---|
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
Timer コンポーネントはサーバー ベースのタイマで、これを使用して Elapsed イベントをアプリケーションで発生させる任意の間隔を指定できます。その後、このイベントを使用して、一定の間隔で処理を行うことができます。たとえば、1 日 24 時間、1 週間 7 日稼働する必要がある重要なサーバーがあるとします。定期的にサーバーをチェックし、システムが稼働状態であることを確認するために Timer を使用するサービスを作成できます。システムが応答しない場合は、このサービスによりサーバーが再起動されるか、または管理者に通知されます。
サーバー ベースの Timer は、マルチスレッド環境においてワーカー スレッドと共に使用するようにデザインされています。サーバー タイマはスレッド間を移動して、発生した Elapsed イベントを処理できます。このため、イベントを時間どおりに発生させるという点で、Windows のタイマより正確です。サーバー ベースのタイマの詳細については、「サーバー ベースのタイマの概説」を参照してください。
Timer コンポーネントは、Interval プロパティの値を基に Elapsed イベントを発生させます。このイベントを処理して、必要な処理を実行できます。たとえば、継続的にデータベースに売上をポストするオンラインのセールス アプリケーションがあるとします。配送の指示をコンパイルするサービスは、オーダーを個別にではなくバッチで処理します。Timer を使用すると、30 分ごとにバッチ処理を開始できます。
![]() |
---|
AutoReset が false に設定されている場合、Timer は最初の Interval が経過した後に、Elapsed イベントを 1 回だけ発生させます。Interval で Elapsed イベントを繰り返し発生させるには、AutoReset を true に設定します。 |
Elapsed イベントは、ThreadPool スレッドで発生します。Elapsed イベントの処理が Interval よりも長びくと、別の ThreadPool スレッドで同じイベントが再発生する場合があります。このため、イベント ハンドラは再入可能であることが必要です。
![]() |
---|
1 つのスレッドでイベント処理メソッドを実行している間に、別のスレッドで Stop メソッドを呼び出したり、Enabled プロパティを false に設定したりする可能性があります。これにより、タイマが停止した後に Elapsed イベントが発生する場合があります。Stop メソッドのコード例では、この競合状態を回避する方法の一例を示しています。 |
Timer を、フォームやコントロールなどのユーザー インターフェイス要素で使用する場合は、Timer を含むフォームまたはコントロールを SynchronizingObject プロパティに割り当てて、イベントがユーザー インターフェイス スレッドにマーシャリングされるようにする必要があります。

5 秒ごとに、コンソールに "Hello World!" と表示する Timer を作成する例を次に示します。
この例では、System.Timers 名前空間を使用します。
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(); }

System.MarshalByRefObject
System.ComponentModel.Component
System.Timers.Timer


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


Timer は、ユーザー定義の間隔でイベントを発生させるために使用されます。この Windows タイマは、UI スレッドを使用して処理を実行するシングルスレッド環境に合わせて設計されています。ユーザー コードには利用できる UI メッセージ ポンプが必要です。また、このコードは必ず同じスレッドから操作し、別のスレッドに対する呼び出しをマーシャリングする必要があります。
このタイマを使用するときは、Tick イベントを使用して、ポーリング操作を実行するか、スプラッシュ スクリーンを指定した時間だけ表示します。Enabled プロパティが true に設定され、Interval プロパティが 0 を超える場合、Tick イベントは、常に Interval プロパティの設定値に基づいた間隔で発生します。
このクラスには、タイマの間隔、開始および終了を設定するメソッドが用意されています。
![]() |
---|
Windows フォームのタイマ コンポーネントはシングルスレッドで、精度の上限は 55 ミリ秒になります。より精度の高いマルチスレッド タイマが必要な場合は、System.Timers 名前空間の Timer クラスを使用してください。 |

5 秒ごとにアラームを発生させる単純な間隔タイマを実装する例を次に示します。アラームが発生すると、MessageBox にアラームが起動した回数が表示され、タイマの実行を継続するかどうかをユーザーに問い合わせるメッセージが表示されます。
Public Class Class1 Private Shared myTimer As New System.Windows.Forms.Timer() Private Shared alarmCounter As Integer = 1 Private Shared exitFlag As Boolean = False ' This is the method to run when the timer is raised. Private Shared Sub TimerEventProcessor(myObject As Object, _ myEventArgs As EventArgs) myTimer.Stop() ' Displays a message box asking whether to continue running the timer. If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _ MessageBoxButtons.YesNo) = DialogResult.Yes Then ' Restarts the timer and increments the counter. alarmCounter += 1 myTimer.Enabled = True Else ' Stops the timer. exitFlag = True End If End Sub Public Shared Sub Main() ' Adds the event and the event handler for the method that will ' process the timer event to the timer. AddHandler myTimer.Tick, AddressOf TimerEventProcessor ' Sets the timer interval to 5 seconds. myTimer.Interval = 5000 myTimer.Start() ' Runs the timer, and raises the event. While exitFlag = False ' Processes all the events in the queue. Application.DoEvents() End While End Sub End Class
public class Class1 { static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); static int alarmCounter = 1; static bool exitFlag = false; // This is the method to run when the timer is raised. private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { myTimer.Stop(); // Displays a message box asking whether to continue running the timer. if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, MessageBoxButtons.YesNo) == DialogResult.Yes) { // Restarts the timer and increments the counter. alarmCounter +=1; myTimer.Enabled = true; } else { // Stops the timer. exitFlag = true; } } public static int Main() { /* Adds the event and the event handler for the method that will process the timer event to the timer. */ myTimer.Tick += new EventHandler(TimerEventProcessor); // Sets the timer interval to 5 seconds. myTimer.Interval = 5000; myTimer.Start(); // Runs the timer, and raises the event. while(exitFlag == false) { // Processes all the events in the queue. Application.DoEvents(); } return 0; } }
public ref class Class1 { private: static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer; static int alarmCounter = 1; static bool exitFlag = false; // This is the method to run when the timer is raised. static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ ) { myTimer->Stop(); // Displays a message box asking whether to continue running the timer. if ( MessageBox::Show( "Continue running?", String::Format( "Count is: {0}", alarmCounter ), MessageBoxButtons::YesNo ) == DialogResult::Yes ) { // Restarts the timer and increments the counter. alarmCounter += 1; myTimer->Enabled = true; } else { // Stops the timer. exitFlag = true; } } public: static void Main() { /* Adds the event and the event handler for the method that will process the timer event to the timer. */ myTimer->Tick += gcnew EventHandler( TimerEventProcessor ); // Sets the timer interval to 5 seconds. myTimer->Interval = 5000; myTimer->Start(); // Runs the timer, and raises the event. while ( exitFlag == false ) { // Processes all the events in the queue. Application::DoEvents(); } } }; int main() { Class1::Main(); }
public class Class1 { private static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); private static int alarmCounter = 1; private static boolean exitFlag = false; // This is the method to run when the timer is raised. private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { myTimer.Stop(); // Displays a message box asking whether to continue running the timer. if (MessageBox.Show("Continue running?", "Count is: " + alarmCounter, MessageBoxButtons.YesNo).Equals(DialogResult.Yes)) { // Restarts the timer and increments the counter. alarmCounter += 1; myTimer.set_Enabled(true); } else { // Stops the timer. exitFlag = true; } } //TimerEventProcessor public static void main(String[] args) { /* Adds the event and the event handler for the method that will process the timer event to the timer. */ myTimer.add_Tick(new EventHandler(TimerEventProcessor)); // Sets the timer interval to 5 seconds. myTimer.set_Interval(5000); myTimer.Start(); // Runs the timer, and raises the event. while (exitFlag == false) { // Processes all the events in the queue. Application.DoEvents(); } return; } //main } //Class1

System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Timer


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


Timer コンストラクタ ()
アセンブリ: System (system.dll 内)



5 秒ごとに、コンソールに "Hello World!" と表示する Timer を作成する例を次に示します。
この例では、System.Timers 名前空間を使用します。
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer コンストラクタ ()
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


新しいタイマが作成されると、タイマは無効になります。つまり、Enabled が false に設定されます。タイマを有効にするには、Start メソッドを呼び出すか、Enabled を true に設定します。
タイマは、無効になり、適用範囲外になると、ガベージ コレクションの発生時に破棄されます。タイマが有効な場合は、適用範囲外であっても、ガベージ コレクションの対象ではありません。

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


Timer コンストラクタ (IContainer)
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


新しいタイマが作成されると、タイマは無効になります。つまり、Enabled が false に設定されます。タイマを有効にするには、Start メソッドを呼び出すか、Enabled を true に設定します。

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


Timer コンストラクタ

名前 | 説明 |
---|---|
Timer () | Timer クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Timer (IContainer) | Timer クラスの新しいインスタンスと、指定したコンテナを初期化します。 |

Timer コンストラクタ (TimerCallback, Object, UInt32, UInt32)
アセンブリ: mscorlib (mscorlib.dll 内)

<CLSCompliantAttribute(False)> _ Public Sub New ( _ callback As TimerCallback, _ state As Object, _ dueTime As UInteger, _ period As UInteger _ )
Dim callback As TimerCallback Dim state As Object Dim dueTime As UInteger Dim period As UInteger Dim instance As New Timer(callback, state, dueTime, period)
[CLSCompliantAttribute(false)] public Timer ( TimerCallback callback, Object state, uint dueTime, uint period )
[CLSCompliantAttribute(false)] public: Timer ( TimerCallback^ callback, Object^ state, unsigned int dueTime, unsigned int period )
/** @attribute CLSCompliantAttribute(false) */ public Timer ( TimerCallback callback, Object state, UInt32 dueTime, UInt32 period )
CLSCompliantAttribute(false) public function Timer ( callback : TimerCallback, state : Object, dueTime : uint, period : uint )


callback パラメータで指定されたデリゲートは dueTime が経過した後一度呼び出されます。その後は period の時間間隔が経過するごとに呼び出されます。
![]() |
---|
Visual Basic のユーザーは TimerCallback コンストラクタを省略できます。コールバック メソッドを指定するときは単純に AddressOf 演算子を使用できます。Visual Basic は正しいデリゲート コンストラクタを自動的に呼び出します。 |
dueTime が 0 の場合、callback はすぐに呼び出されます。dueTime が Timeout.Infinite の場合、callback は呼び出されません。タイマは無効になっていますが、Change メソッドを呼び出すことによって再有効化できます。
period が 0 または Infinite で、dueTime が Infinite でない場合、callback は一度呼び出されます。タイマの定期的な動作は無効になっていますが、Change メソッドを使用することによって再有効化できます。
callback に指定されたメソッドは、ThreadPool スレッドで呼び出されるため、再入可能である必要があります。タイマ間隔がメソッドの実行に必要な時間よりも小さい場合、または、スレッド プール スレッドがすべて使用中でメソッドが複数回キューに置かれる場合は、メソッドを 2 つのスレッド プール スレッドで同時に実行できます。

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer コンストラクタ (TimerCallback, Object, Int32, Int32)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ callback As TimerCallback, _ state As Object, _ dueTime As Integer, _ period As Integer _ )
Dim callback As TimerCallback Dim state As Object Dim dueTime As Integer Dim period As Integer Dim instance As New Timer(callback, state, dueTime, period)


callback パラメータで指定されたデリゲートは dueTime が経過した後一度呼び出されます。その後は period の時間間隔が経過するごとに呼び出されます。
![]() |
---|
Visual Basic のユーザーは TimerCallback コンストラクタを省略できます。コールバック メソッドを指定するときは単純に AddressOf 演算子を使用できます。Visual Basic は正しいデリゲート コンストラクタを自動的に呼び出します。 |
dueTime が 0 の場合、callback はすぐに呼び出されます。dueTime が Timeout.Infinite の場合、callback は呼び出されません。タイマは無効になっていますが、Change メソッドを呼び出すことによって再有効化できます。
period が 0 または Infinite で、dueTime が Infinite でない場合、callback は一度呼び出されます。タイマの定期的な動作は無効になっていますが、Change メソッドを使用することによって再有効化できます。
callback に指定されたメソッドは、ThreadPool スレッドで呼び出されるため、再入可能である必要があります。タイマ間隔がメソッドの実行に必要な時間よりも小さい場合、または、スレッド プール スレッドがすべて使用中でメソッドが複数回キューに置かれる場合は、メソッドを 2 つのスレッド プール スレッドで同時に実行できます。

TimerCallback デリゲートを作成し、Timer クラスの新しいインスタンスを初期化する例を次に示します。
Imports Microsoft.VisualBasic Imports System Imports System.Threading Public Class TimerExample <MTAThread> _ Shared Sub Main() Dim autoEvent As New AutoResetEvent(False) Dim statusChecker As New StatusChecker(10) ' Create the delegate that invokes methods for the timer. Dim timerDelegate As TimerCallback = _ AddressOf statusChecker.CheckStatus ' Create a timer that signals the delegate to invoke ' CheckStatus after one second, and every 1/4 second ' thereafter. Console.WriteLine("{0} Creating timer." & vbCrLf, _ DateTime.Now.ToString("h:mm:ss.fff")) Dim stateTimer As Timer = _ New Timer(timerDelegate, autoEvent, 1000, 250) ' When autoEvent signals, change the period to every ' 1/2 second. autoEvent.WaitOne(5000, False) stateTimer.Change(0, 500) Console.WriteLine(vbCrLf & "Changing period." & vbCrLf) ' When autoEvent signals the second time, dispose of ' the timer. autoEvent.WaitOne(5000, False) stateTimer.Dispose() Console.WriteLine(vbCrLf & "Destroying timer.") End Sub End Class Public Class StatusChecker Dim invokeCount, maxCount As Integer Sub New(count As Integer) invokeCount = 0 maxCount = count End Sub ' This method is called by the timer delegate. Sub CheckStatus(stateInfo As Object) Dim autoEvent As AutoResetEvent = _ DirectCast(stateInfo, AutoResetEvent) invokeCount += 1 Console.WriteLine("{0} Checking status {1,2}.", _ DateTime.Now.ToString("h:mm:ss.fff"), _ invokeCount.ToString()) If invokeCount = maxCount Then ' Reset the counter and signal to stop the timer. invokeCount = 0 autoEvent.Set() End If End Sub End Class
using System; using System.Threading; class TimerExample { static void Main() { AutoResetEvent autoEvent = new AutoResetEvent(false); StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus); // Create a timer that signals the delegate to invoke // CheckStatus after one second, and every 1/4 second // thereafter. Console.WriteLine("{0} Creating timer.\n", DateTime.Now.ToString("h:mm:ss.fff")); Timer stateTimer = new Timer(timerDelegate, autoEvent, 1000, 250); // When autoEvent signals, change the period to every // 1/2 second. autoEvent.WaitOne(5000, false); stateTimer.Change(0, 500); Console.WriteLine("\nChanging period.\n"); // When autoEvent signals the second time, dispose of // the timer. autoEvent.WaitOne(5000, false); stateTimer.Dispose(); Console.WriteLine("\nDestroying timer."); } } class StatusChecker { int invokeCount, maxCount; public StatusChecker(int count) { invokeCount = 0; maxCount = count; } // This method is called by the timer delegate. public void CheckStatus(Object stateInfo) { AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; Console.WriteLine("{0} Checking status {1,2}.", DateTime.Now.ToString("h:mm:ss.fff"), (++invokeCount).ToString()); if(invokeCount == maxCount) { // Reset the counter and signal Main. invokeCount = 0; autoEvent.Set(); } } }
using namespace System; using namespace System::Threading; ref class StatusChecker { private: int invokeCount; int maxCount; public: StatusChecker( int count ) : invokeCount( 0 ), maxCount( count ) {} // This method is called by the timer delegate. void CheckStatus( Object^ stateInfo ) { AutoResetEvent^ autoEvent = dynamic_cast<AutoResetEvent^>(stateInfo); Console::WriteLine( "{0} Checking status {1,2}.", DateTime::Now.ToString( "h:mm:ss.fff" ), (++invokeCount).ToString() ); if ( invokeCount == maxCount ) { // Reset the counter and signal main. invokeCount = 0; autoEvent->Set(); } } }; int main() { AutoResetEvent^ autoEvent = gcnew AutoResetEvent( false ); StatusChecker^ statusChecker = gcnew StatusChecker( 10 ); // Create the delegate that invokes methods for the timer. TimerCallback^ timerDelegate = gcnew TimerCallback( statusChecker, &StatusChecker::CheckStatus ); // Create a timer that signals the delegate to invoke CheckStatus // after one second, and every 1/4 second thereafter. Console::WriteLine( "{0} Creating timer.\n", DateTime::Now.ToString( "h:mm:ss.fff" ) ); Timer^ stateTimer = gcnew Timer( timerDelegate,autoEvent,1000,250 ); // When autoEvent signals, change the period to every 1/2 second. autoEvent->WaitOne( 5000, false ); stateTimer->Change( 0, 500 ); Console::WriteLine( "\nChanging period.\n" ); // When autoEvent signals the second time, dispose of the timer. autoEvent->WaitOne( 5000, false ); stateTimer->~Timer(); Console::WriteLine( "\nDestroying timer." ); }
import System.*; import System.Threading.*; import System.Threading.Thread; class TimerExample { public static void main(String[] args) { AutoResetEvent autoEvent = new AutoResetEvent(false); StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback( statusChecker.CheckStatus); // Create a timer that signals the delegate to invoke // CheckStatus after one second, and every 1/4 second // thereafter. Console.WriteLine("{0} Creating timer.\n", System.DateTime.get_Now().ToString("h:mm:ss.fff")); Timer stateTimer = new Timer(timerDelegate, autoEvent, 1000, 250); // When autoEvent signals, change the period to every // 1/2 second. autoEvent.WaitOne(5000, false); stateTimer.Change(0, 500); Console.WriteLine("\nChanging period.\n"); // When autoEvent signals the second time, dispose of // the timer. autoEvent.WaitOne(5000, false); stateTimer.Dispose(); Console.WriteLine("\nDestroying timer."); } //main } //TimerExample class StatusChecker { private int invokeCount, maxCount; public StatusChecker(int count) { invokeCount = 0; maxCount = count; } //StatusChecker // This method is called by the timer delegate. public void CheckStatus(Object stateInfo) { AutoResetEvent autoEvent = ((AutoResetEvent)(stateInfo)); Console.WriteLine("{0} Checking status {1,2}.", System.DateTime.get_Now().ToString("h:mm:ss.fff"), String.valueOf(++invokeCount)); if (invokeCount == maxCount) { // Reset the counter and signal Main. invokeCount = 0; autoEvent.Set(); } } //CheckStatus } //StatusChecker

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer コンストラクタ (TimerCallback, Object, Int64, Int64)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ callback As TimerCallback, _ state As Object, _ dueTime As Long, _ period As Long _ )
Dim callback As TimerCallback Dim state As Object Dim dueTime As Long Dim period As Long Dim instance As New Timer(callback, state, dueTime, period)


callback パラメータで指定されたデリゲートは dueTime が経過した後一度呼び出されます。その後は period の時間間隔が経過するごとに呼び出されます。
![]() |
---|
Visual Basic のユーザーは TimerCallback コンストラクタを省略できます。コールバック メソッドを指定するときは単純に AddressOf 演算子を使用できます。Visual Basic は正しいデリゲート コンストラクタを自動的に呼び出します。 |
dueTime が 0 の場合、callback はすぐに呼び出されます。dueTime が Timeout.Infinite の場合、callback は呼び出されません。タイマは無効になっていますが、Change メソッドを呼び出すことによって再有効化できます。
period が 0 または Infinite で、dueTime が Infinite でない場合、callback は一度呼び出されます。タイマの定期的な動作は無効になっていますが、Change メソッドを使用することによって再有効化できます。
callback に指定されたメソッドは、ThreadPool スレッドで呼び出されるため、再入可能である必要があります。タイマ間隔がメソッドの実行に必要な時間よりも小さい場合、または、スレッド プール スレッドがすべて使用中でメソッドが複数回キューに置かれる場合は、メソッドを 2 つのスレッド プール スレッドで同時に実行できます。

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer コンストラクタ (TimerCallback, Object, TimeSpan, TimeSpan)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ callback As TimerCallback, _ state As Object, _ dueTime As TimeSpan, _ period As TimeSpan _ )
Dim callback As TimerCallback Dim state As Object Dim dueTime As TimeSpan Dim period As TimeSpan Dim instance As New Timer(callback, state, dueTime, period)
public function Timer ( callback : TimerCallback, state : Object, dueTime : TimeSpan, period : TimeSpan )


callback パラメータで指定されたデリゲートは dueTime が経過した後一度呼び出されます。その後は period の時間間隔が経過するごとに呼び出されます。
![]() |
---|
Visual Basic のユーザーは TimerCallback コンストラクタを省略できます。コールバック メソッドを指定するときは単純に AddressOf 演算子を使用できます。Visual Basic は正しいデリゲート コンストラクタを自動的に呼び出します。 |
dueTime が 0 の場合、callback はすぐに呼び出されます。dueTime が -1 ミリ秒の場合、callback は呼び出されません。タイマは無効になっていますが、Change メソッドを呼び出すことによって再有効化できます。
period が 0 または -1 ミリ秒で、dueTime が正の値の場合、callback は一度呼び出されます。タイマの定期的な動作は無効になっていますが、Change メソッドを使用することによって再有効化できます。
callback に指定されたメソッドは、ThreadPool スレッドで呼び出されるため、再入可能である必要があります。タイマ間隔がメソッドの実行に必要な時間よりも小さい場合、または、スレッド プール スレッドがすべて使用中でメソッドが複数回キューに置かれる場合は、メソッドを 2 つのスレッド プール スレッドで同時に実行できます。

TimerCallback デリゲートを作成し、Timer クラスの新しいインスタンスを初期化する例を次に示します。
Imports Microsoft.VisualBasic Imports System Imports System.Threading Public Class TimerExample <MTAThread> _ Shared Sub Main() Dim autoEvent As New AutoResetEvent(False) Dim statusChecker As New StatusChecker(10) ' Create the delegate that invokes methods for the timer. Dim timerDelegate As TimerCallback = _ AddressOf statusChecker.CheckStatus Dim delayTime As New TimeSpan(0, 0, 1) Dim intervalTime As New TimeSpan(0, 0, 0, 0, 250) ' Create a timer that signals the delegate to invoke ' CheckStatus after one second, and every 1/4 second ' thereafter. Console.WriteLine("{0} Creating timer." & vbCrLf, _ DateTime.Now.ToString("h:mm:ss.fff")) Dim stateTimer As Timer = New Timer( _ timerDelegate, autoEvent, delayTime, intervalTime) ' When autoEvent signals, change the period to every ' 1/2 second. autoEvent.WaitOne(5000, False) stateTimer.Change( _ new TimeSpan(0), intervalTime.Add(intervalTime)) Console.WriteLine(vbCrLf & "Changing period." & vbCrLf) ' When autoEvent signals the second time, dispose of ' the timer. autoEvent.WaitOne(5000, False) stateTimer.Dispose() Console.WriteLine(vbCrLf & "Destroying timer.") End Sub End Class Public Class StatusChecker Dim invokeCount, maxCount As Integer Sub New(count As Integer) invokeCount = 0 maxCount = count End Sub ' This method is called by the timer delegate. Sub CheckStatus(stateInfo As Object) Dim autoEvent As AutoResetEvent = _ DirectCast(stateInfo, AutoResetEvent) invokeCount += 1 Console.WriteLine("{0} Checking status {1,2}.", _ DateTime.Now.ToString("h:mm:ss.fff"), _ invokeCount.ToString()) If invokeCount = maxCount Then ' Reset the counter and signal to stop the timer. invokeCount = 0 autoEvent.Set() End If End Sub End Class
using System; using System.Threading; class TimerExample { static void Main() { AutoResetEvent autoEvent = new AutoResetEvent(false); StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus); TimeSpan delayTime = new TimeSpan(0, 0, 1); TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 250); // Create a timer that signals the delegate to invoke // CheckStatus after one second, and every 1/4 second // thereafter. Console.WriteLine("{0} Creating timer.\n", DateTime.Now.ToString("h:mm:ss.fff")); Timer stateTimer = new Timer( timerDelegate, autoEvent, delayTime, intervalTime); // When autoEvent signals, change the period to every // 1/2 second. autoEvent.WaitOne(5000, false); stateTimer.Change(new TimeSpan(0), intervalTime + intervalTime); Console.WriteLine("\nChanging period.\n"); // When autoEvent signals the second time, dispose of // the timer. autoEvent.WaitOne(5000, false); stateTimer.Dispose(); Console.WriteLine("\nDestroying timer."); } } class StatusChecker { int invokeCount, maxCount; public StatusChecker(int count) { invokeCount = 0; maxCount = count; } // This method is called by the timer delegate. public void CheckStatus(Object stateInfo) { AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; Console.WriteLine("{0} Checking status {1,2}.", DateTime.Now.ToString("h:mm:ss.fff"), (++invokeCount).ToString()); if(invokeCount == maxCount) { // Reset the counter and signal Main. invokeCount = 0; autoEvent.Set(); } } }
using namespace System; using namespace System::Threading; ref class StatusChecker { private: int invokeCount; int maxCount; public: StatusChecker( int count ) : invokeCount( 0 ), maxCount( count ) {} // This method is called by the timer delegate. void CheckStatus( Object^ stateInfo ) { AutoResetEvent^ autoEvent = dynamic_cast<AutoResetEvent^>(stateInfo); Console::WriteLine( "{0} Checking status {1,2}.", DateTime::Now.ToString( "h:mm:ss.fff" ), (++invokeCount).ToString() ); if ( invokeCount == maxCount ) { // Reset the counter and signal main. invokeCount = 0; autoEvent->Set(); } } }; int main() { AutoResetEvent^ autoEvent = gcnew AutoResetEvent( false ); StatusChecker^ statusChecker = gcnew StatusChecker( 10 ); // Create the delegate that invokes methods for the timer. TimerCallback^ timerDelegate = gcnew TimerCallback( statusChecker, &StatusChecker::CheckStatus ); TimeSpan delayTime = TimeSpan(0,0,1); TimeSpan intervalTime = TimeSpan(0,0,0,0,250); // Create a timer that signals the delegate to invoke CheckStatus // after one second, and every 1/4 second thereafter. Console::WriteLine( "{0} Creating timer.\n", DateTime::Now.ToString( "h:mm:ss.fff" ) ); Timer^ stateTimer = gcnew Timer( timerDelegate,autoEvent,delayTime,intervalTime ); // When autoEvent signals, change the period to every 1/2 second. autoEvent->WaitOne( 5000, false ); stateTimer->Change( TimeSpan(0), intervalTime + intervalTime ); Console::WriteLine( "\nChanging period.\n" ); // When autoEvent signals the second time, dispose of the timer. autoEvent->WaitOne( 5000, false ); stateTimer->~Timer(); Console::WriteLine( "\nDestroying timer." ); }
import System.*; import System.Threading.*; import System.Threading.Thread; class TimerExample { public static void main(String[] args) { AutoResetEvent autoEvent = new AutoResetEvent(false); StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate = new TimerCallback(statusChecker.CheckStatus); TimeSpan delayTime = new TimeSpan(0, 0, 1); TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 250); // Create a timer that signals the delegate to invoke // CheckStatus after one second, and every 1/4 second // thereafter. Console.WriteLine("{0} Creating timer.\n", System.DateTime.get_Now().ToString("h:mm:ss.fff")); Timer stateTimer = new Timer(timerDelegate, autoEvent, delayTime, intervalTime); // When autoEvent signals, change the period to every // 1/2 second. autoEvent.WaitOne(5000, false); stateTimer.Change(new TimeSpan(0), intervalTime.Add(intervalTime)); Console.WriteLine("\nChanging period.\n"); // When autoEvent signals the second time, dispose of // the timer. autoEvent.WaitOne(5000, false); stateTimer.Dispose(); Console.WriteLine("\nDestroying timer."); } //main } //TimerExample class StatusChecker { private int invokeCount, maxCount; public StatusChecker(int count) { invokeCount = 0; maxCount = count; } //StatusChecker // This method is called by the timer delegate. public void CheckStatus(Object stateInfo) { AutoResetEvent autoEvent = ((AutoResetEvent)(stateInfo)); Console.WriteLine("{0} Checking status {1,2}.", System.DateTime.get_Now().ToString("h:mm:ss.fff"), String.valueOf(++invokeCount)); if (invokeCount == maxCount) { // Reset the counter and signal Main. invokeCount = 0; autoEvent.Set(); } } //CheckStatus } //StatusChecker

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer コンストラクタ (Double)
アセンブリ: System (system.dll 内)



10 秒経過すると、コンソールに "Hello World!" と表示する Timer を作成する例を次に示します。
この例では、System.Timers 名前空間を使用します。
' From command line, compile with /r:System.dll Imports System Imports System.Timers Public Class Timer2 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. ' ' Create a timer with a ten second interval. Dim aTimer As New System.Timers.Timer(10000) ' Hook up the event handler for the Elapsed event. AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Only raise the event the first time Interval elapses. aTimer.AutoReset = False 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
// From command line, compile with /r:System.dll using System; using System.Timers; public class Timer2 { 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. // // Create a timer with a ten second interval. System.Timers.Timer aTimer = new System.Timers.Timer(10000); // Hook up the event handler for the Elapsed event. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); // Only raise the event the first time Interval elapses. aTimer.AutoReset = false; 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 Timer2 { 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 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. // // Create a new Timer with Interval set to 10 seconds. System::Timers::Timer^ aTimer = gcnew System::Timers::Timer( 10000 ); // Hook up the event handler for the Elapsed event. aTimer->Elapsed += gcnew ElapsedEventHandler( OnTimedEvent ); // Only raise the event the first time Interval elapses. aTimer->AutoReset = false; 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() { Timer2::Main(); }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer コンストラクタ (TimerCallback)
アセンブリ: mscorlib (mscorlib.dll 内)


このコンストラクタは、Timer オブジェクト自体を状態オブジェクトとして使用する場合に呼び出します。タイマを作成した後で、Change メソッドを使用して期間と期限を設定します。
このコンストラクタは、Timer オブジェクトが状態オブジェクトに割り当てられる前に最初のコールバックが発生するのを防ぐために、最初のコールバックまでの期限およびコールバックの間隔を無制限に指定します。
callback に指定されたメソッドは、ThreadPool スレッドで呼び出されるため、再入可能である必要があります。タイマ間隔がメソッドの実行に必要な時間よりも小さい場合、または、スレッド プール スレッドがすべて使用中でメソッドが複数回キューに置かれる場合は、メソッドを 2 つのスレッド プール スレッドで同時に実行できます。

タイマ自体を状態オブジェクトとして使用して新しいタイマを作成するコード例を次に示します。Change メソッドは、タイマを開始するために使用します。タイマのコールバックが発生すると、状態オブジェクトを使用してタイマがオフにされます。
Imports System Imports System.Threading Public Class Example Public Shared Sub Main() ' Create an instance of the Example class, and start two ' timers. Dim ex As New Example() ex.StartTimer(2000) ex.StartTimer(1000) Console.WriteLine("Press Enter to end the program.") Console.ReadLine() End Sub Public Sub StartTimer(ByVal dueTime As Integer) Dim t As New Timer(AddressOf TimerProc) t.Change(dueTime, 0) End Sub Private Sub TimerProc(ByVal state As Object) ' The state object is the Timer object. Dim t As Timer = CType(state, Timer) t.Dispose() Console.WriteLine("The timer callback executes.") End Sub End Class
using System; using System.Threading; public class Example { public static void Main() { // Create an instance of the Example class, and start two // timers. Example ex = new Example(); ex.StartTimer(2000); ex.StartTimer(1000); Console.WriteLine("Press Enter to end the program."); Console.ReadLine(); } public void StartTimer(int dueTime) { Timer t = new Timer(new TimerCallback(TimerProc)); t.Change(dueTime, 0); } private void TimerProc(object state) { // The state object is the Timer object. Timer t = (Timer) state; t.Dispose(); Console.WriteLine("The timer callback executes."); } }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Timer コンストラクタ

名前 | 説明 |
---|---|
Timer (TimerCallback) | 状態オブジェクトとして新しく作成した Timer を使用して、無制限の期間および無制限の期限を指定して Timer クラスの新しいインスタンスを初期化します。 |
Timer (TimerCallback, Object, Int32, Int32) | 時間間隔を指定するために 32 ビット符号付き整数を使用して、Timer クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Timer (TimerCallback, Object, Int64, Int64) | 時間間隔を計るために 64 ビット符号付き整数を使用して、Timer クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Timer (TimerCallback, Object, TimeSpan, TimeSpan) | 時間間隔を計るために TimeSpan 値を使用して、Timer クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Timer (TimerCallback, Object, UInt32, UInt32) | 時間間隔を計るために 32 ビット符号なし整数を使用して、Timer クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |

Timer コンストラクタ

名前 | 説明 |
---|---|
Timer () | Timer クラスの新しいインスタンスを初期化し、すべてのプロパティを初期値に設定します。 |
Timer (Double) | Timer クラスの新しいインスタンスを初期化し、Interval プロパティを指定した間隔に設定します。 |

Timer プロパティ

名前 | 説明 | |
---|---|---|
![]() | AutoReset | 指定した間隔が経過するたびに、または経過した後に 1 回だけ、Timer で Elapsed イベントを発生させるかどうかを示す値を取得または設定します。 |
![]() | Container | Component を格納している IContainer を取得します。 ( Component から継承されます。) |
![]() | Enabled | Timer で Elapsed イベントを発生させる必要があるかどうかを示す値を取得または設定します。 |
![]() | Interval | Elapsed イベントの発生間隔を取得または設定します。 |
![]() | Site | オーバーライドされます。 デザイン モードで Timer をコンテナにバインドするサイトを取得または設定します。 |
![]() | SynchronizingObject | 指定した間隔が経過したときに発行されるイベント ハンドラ呼び出しをマーシャリングするために使用するオブジェクトを取得または設定します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。 ( Component から継承されます。) |

Timer プロパティ

名前 | 説明 | |
---|---|---|
![]() | Container | Component を格納している IContainer を取得します。 ( Component から継承されます。) |
![]() | Site | Component の ISite を取得または設定します。 ( Component から継承されます。) |
![]() | Tag | なんらかの種類のユーザー状態を表す任意の文字列を取得または設定します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。 ( Component から継承されます。) |

Timer メソッド

名前 | 説明 | |
---|---|---|
![]() | Change | オーバーロードされます。 タイマの開始時刻とメソッドの呼び出しの間隔を変更します。 |
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。) |
![]() | Dispose | オーバーロードされます。 Timer の現在のインスタンスによって使用されているすべてのリソースを解放します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

Timer メソッド


名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 現在の Timer によって使用されているすべてのリソースを解放します。 |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 ( Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 ( Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

Timer メソッド

名前 | 説明 | |
---|---|---|
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。) |
![]() | Dispose | オーバーロードされます。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | Start | タイマを起動します。 |
![]() | Stop | タイマを停止します。 |
![]() | ToString | オーバーライドされます。 Timer を表す文字列を返します。 |

名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 ( Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 ( Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnTick | Tick イベントを発生させます。 |

Timer メンバ
指定した間隔でメソッドを実行するための機構を提供します。このクラスは継承できません。
Timer データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Change | オーバーロードされます。 タイマの開始時刻とメソッドの呼び出しの間隔を変更します。 |
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (MarshalByRefObject から継承されます。) |
![]() | Dispose | オーバーロードされます。 Timer の現在のインスタンスによって使用されているすべてのリソースを解放します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

Timer メンバ
Timer データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | AutoReset | 指定した間隔が経過するたびに、または経過した後に 1 回だけ、Timer で Elapsed イベントを発生させるかどうかを示す値を取得または設定します。 |
![]() | Container | Component を格納している IContainer を取得します。(Component から継承されます。) |
![]() | Enabled | Timer で Elapsed イベントを発生させる必要があるかどうかを示す値を取得または設定します。 |
![]() | Interval | Elapsed イベントの発生間隔を取得または設定します。 |
![]() | Site | オーバーライドされます。 デザイン モードで Timer をコンテナにバインドするサイトを取得または設定します。 |
![]() | SynchronizingObject | 指定した間隔が経過したときに発行されるイベント ハンドラ呼び出しをマーシャリングするために使用するオブジェクトを取得または設定します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。(Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。(Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。(Component から継承されます。) |


名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 現在の Timer によって使用されているすべてのリソースを解放します。 |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 (Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 (Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。(Component から継承されます。) |
![]() | Elapsed | 間隔が経過すると発生します。 |

Timer メンバ
ユーザー定義の間隔でイベントを発生させるタイマを実装します。このタイマは、Windows フォーム アプリケーションで使用できるように最適化されていて、ウィンドウで使用する必要があります。
Timer データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Container | Component を格納している IContainer を取得します。(Component から継承されます。) |
![]() | Site | Component の ISite を取得または設定します。(Component から継承されます。) |
![]() | Tag | なんらかの種類のユーザー状態を表す任意の文字列を取得または設定します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。(Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。(Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。(Component から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (MarshalByRefObject から継承されます。) |
![]() | Dispose | オーバーロードされます。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | Start | タイマを起動します。 |
![]() | Stop | タイマを停止します。 |
![]() | ToString | オーバーライドされます。 Timer を表す文字列を返します。 |

名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 (Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 (Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnTick | Tick イベントを発生させます。 |

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。(Component から継承されます。) |
![]() | Tick | 指定したタイマの間隔が経過し、タイマが有効である場合に発生します。 |

タイマー



タイマー(英: timer)は、「時間を測る」という意味を持つ動詞timeを動作主名詞化した英単語で、以下のような意味を持つ。
- 経過時間を知らせる仕組みや装置[1]。この意味については、漢字表現では「計時機」や「計時器」とも言うことがある。
- あらかじめ設定した時刻に何らかの動作を行う装置[1]。定期的に電源をオン・オフするもの。その意味で目覚まし時計なども含められることがある。
概説
単体のカウントダウン式のタイマーだけでなく、家電機器に組み込まれた「予約動作機能」もタイマーである。多機能時計の場合、経過時間測定機能だけでなく、目覚まし機能や、正時ごとの時報音もタイマーに分類されうる。
なおベルやブザーなどの音で知らせるものだけでなく、音がしない砂時計もタイマーである。
単体のキッチンタイマー(en:Egg timer)などは50年ほど前[いつ?]までは、ゼンマイ式でベル(鈴)を鳴らして知らせる機械式のアナログ装置ばかりだったが、その後、電池とエレクトロニクス技術を使いブザーや発光で知らせるものが一般的となり、また小型のモノクロ液晶ディスプレイでデジタル表示され、ボタン電池で動きアラームが鳴るものが登場した。品質や耐久性を問わなければ、ワンコインショップ(100円ショップ、百均)でも入手できるようになっている[2][3]。
汎用コンピュータでは、さまざまなレベル(階層)においてタイマー機能が組み込まれていて、それにより多種多様、きめ細かな動作を実現している。たとえばマザーボードやBIOSといったハードウェア・ファームウェアレベルだけでなく、オペレーティングシステム (OS) やプログラミング言語の標準ライブラリ、アプリケーションフレームワークやアプリケーションソフトウェアといったソフトウェアレベルでもタイマーが実装・利用される。タイマーの精度および分解能は実装形態に依存する。スマートフォンなどのOSにも目覚まし時計・タイマー・ストップウォッチの機能を併せ持つアプリが標準搭載されていることが多い。
なお、工業分野やコンピュータ分野では、以前は古いJIS規格(JIS Z8301)の影響で長音符「ー」を省略した「タイマ」という表記が使用されることが多かった(JIS Z8301:2008まで)。その後改訂されたJIS Z8301:2019では規則が変更され、「外来語の表記は,主として“外来語の表記(平成3.6.28 内閣告示第2号)”による。」となった。この内閣告示は「原則として長音符を用いて書き表すが、慣用に応じて長音符を省くことができる」としている。そのため、技術系の分野でも「タイマー」というように「ー」をつけることが増えている。
作動方式による種別
- オンディレイタイマー
- 入力信号がオンし続けた場合に、セット時間が経過すると作動するタイマーの方式。
- オフディレイタイマー
- 入力信号がオンからオフに変わった場合に、セット時間が経過すると作動するタイマーの方式。
- フリッカータイマー
- 入力信号がオンの場合に、セット時間でオンとオフを繰り返すタイマー方式。
単体のタイマー
- キッチンタイマー
- 調理時間の目安とするためのタイマーである。設定時間が経過した後にブザーが鳴る。電子回路を用いたデジタル式の製品が普及しているが、ぜんまいばねを応用した、簡単なダイヤル操作だけで使用できるアナログ式の製品も使われている。デジタル式はカウントアップによる簡易ストップウォッチになる物が多い。また、デジタル式でリセット・ボタンのない物には、“分”設定ボタンと“秒”設定ボタンを同時押しすることによって設定時間をゼロ・リセットできるものがある。
- 24時間繰り返しタイマー
- 照明や生物の飼育などのために、1日のうち決まった時間に動作を繰り返すタイマー。登場初期には時計のような文字盤の周りの爪を動かすことで時間を設定する製品が多く見られたがこれは内部に可動部分(電動時計)を持っており、半導体技術の発達に伴い可動部を持たない低原価の電子式が登場した。
- 表示盤付きタイマー
- スポーツの試合や、持ち時間が決められている口頭発表などで、遠く離れた位置でも見えるよう、大型の表示盤に残り時間などを表示するもの。スポーツの試合用のものでは、タイマー表示(試合時間表示)のほか、得点表示、サーブ権表示などができるものが多い。
機器組み込み型のタイマー

- カメラ
- セルフタイマー - 写真を撮影する際、カメラを操作する人も写真に映ることができるよう一定時間の後にシャッターを作動させるもの。
- シャッタースピードの制御 - シャッターを開きフィルムやセンサに光を当てる時間を指示された一定の長さに調整する仕組み。
- 録画機器、炊飯器、扇風機、エアコンなど家電製品
- 録画・炊飯・暖房など機器動作の予約設定や、電子レンジ・洗濯機などにおけるカウントダウン設定をするため、タイマー(時計)が内蔵されている。
コンピュータ内部のタイマーの例
- ジョブ管理システム
- 設定された日時、曜日時刻などに「ジョブ」の類(コンピュータプログラムやバッチ処理など)の起動や終了を行うソフトウェア。ジョブの動作状況の監視も行う。Microsoft Windowsでは同様のものをタスクスケジューラという。
- インターバルタイマー
- 一定間隔で何らかのシグナルをシステムに対し発生する(一般には割り込みのことが多い)機構。一般にオペレーティングシステム (OS) はリアルタイムクロックで時刻を取得した後はこのインターバルタイマーの割り込みを使って内部時刻をアップデートしていく。OSがプリエンプションの契機として使用することもあれば、OS自身のさまざまな定期処理[注釈 1]や、デバイスドライバなどにOSが提供するタイマー[注釈 2]の元として使われることもある。
- ウォッチドッグタイマー
- コンピュータシステムの正常動作を確認するための機構。OSがウォッチドッグタイマーに対して一定間隔で書き込みを行い、万が一その書き込みが規定期間内に一定回数以上行われなかったときには、システムがハングアップ(具体的にはインターバルタイマーからの割り込みがマスクされた状態で動作し続けている)と判断してシステムをリセットする。組み込みシステムなどで使われることが多い。
脚注
注釈
- ^ ページ置換アルゴリズム、デーモンの定期的処理など。
- ^ 例えば、機器の故障により処理要求がタイムアウトしたと判断するためなど。
出典
関連項目
TIMER
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2021/08/03 08:45 UTC 版)
「Hellsinker.」の記事における「TIMER」の解説
※この「TIMER」の解説は、「Hellsinker.」の解説の一部です。
「TIMER」を含む「Hellsinker.」の記事については、「Hellsinker.」の概要を参照ください。
- Timerのページへのリンク