時間間隔を指定するために 32 ビット符号付き整数を使用して、Timer クラスの新しいインスタンスを初期化します。
名前空間: System.Threading
アセンブリ: 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)
public Timer (
TimerCallback callback,
Object state,
int dueTime,
int period
)
public:
Timer (
TimerCallback^ callback,
Object^ state,
int dueTime,
int period
)
public Timer (
TimerCallback callback,
Object state,
int dueTime,
int period
)
public function Timer (
callback : TimerCallback,
state : Object,
dueTime : int,
period : int
)
適用できません。
パラメータ
- callback
実行するメソッドを表す TimerCallback デリゲート。
- state
コールバック メソッドで使用される情報を格納するオブジェクト。または null 参照 (Visual Basic では Nothing)。
- dueTime
callback が呼び出される前の遅延時間 (ミリ秒単位) です。タイマが開始されないようにするには Timeout.Infinite を指定します。タイマをすぐに開始するには 0 を指定します。
- period
callback が呼び出される時間間隔 (ミリ秒単位) です。周期的なシグナル通知を無効にする Timeout.Infinite を指定します。
例外
例外の種類 | 条件 |
---|---|
dueTime パラメータまたは period パラメータが負の値であり、Infinite と等しくありません。 |
|
callback パラメータが null 参照 (Visual Basic では Nothing) です。 |
解説
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 Server 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
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
バージョン情報
.NET Framework
サポート対象 : 3.0,2.0,1.1,1.0
.NET Compact Framework
サポート対象 : 2.0,1.0
XNA Framework
サポート対象 : 1.0
参照
関連項目
Timer クラス
Timer メンバ
System.Threading 名前空間