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 コンストラクタ ()
アセンブリ: 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 コンストラクタのページへのリンク