次の方法で共有


スレッド タイマー (C# および Visual Basic)

System.Threading.Timer クラスは、タスクを別々のスレッドで定期的に実行するのに便利です。たとえば、スレッド タイマーを使用すると、データベースのステータスと整合性をチェックしたり、重要なファイルをバックアップしたりできます。

スレッド タイマーの例

2 秒ごとにタスクを起動し、フラグを使用して Dispose メソッドでタイマーを停止する例を次に示します。この例は、ステータスを出力ウィンドウに出力します。

Private Class StateObjClass
    ' Used to hold parameters for calls to TimerTask.
    Public SomeValue As Integer
    Public TimerReference As System.Threading.Timer
    Public TimerCanceled As Boolean
End Class

Public Sub RunTimer()
    Dim StateObj As New StateObjClass
    StateObj.TimerCanceled = False
    StateObj.SomeValue = 1
    Dim TimerDelegate As New System.Threading.TimerCallback(AddressOf TimerTask)
    ' Create a timer that calls a procedure every 2 seconds.
    ' Note: There is no Start method; the timer starts running as soon as 
    ' the instance is created.
    Dim TimerItem As New System.Threading.Timer(TimerDelegate, StateObj,
                                                2000, 2000)
    ' Save a reference for Dispose.
    StateObj.TimerReference = TimerItem

    ' Run for ten loops.
    While StateObj.SomeValue < 10
        ' Wait one second.
        System.Threading.Thread.Sleep(1000)
    End While

    ' Request Dispose of the timer object.
    StateObj.TimerCanceled = True
End Sub

Private Sub TimerTask(ByVal StateObj As Object)
    Dim State As StateObjClass = CType(StateObj, StateObjClass)
    ' Use the interlocked class to increment the counter variable.
    System.Threading.Interlocked.Increment(State.SomeValue)
    System.Diagnostics.Debug.WriteLine("Launched new thread  " & Now.ToString)
    If State.TimerCanceled Then
        ' Dispose Requested.
        State.TimerReference.Dispose()
        System.Diagnostics.Debug.WriteLine("Done  " & Now)
    End If
End Sub
private class StateObjClass
{
    // Used to hold parameters for calls to TimerTask.
    public int SomeValue;
    public System.Threading.Timer TimerReference;
    public bool TimerCanceled;
}

public void RunTimer()
{
    StateObjClass StateObj = new StateObjClass();
    StateObj.TimerCanceled = false;
    StateObj.SomeValue = 1;
    System.Threading.TimerCallback TimerDelegate =
        new System.Threading.TimerCallback(TimerTask);

    // Create a timer that calls a procedure every 2 seconds.
    // Note: There is no Start method; the timer starts running as soon as 
    // the instance is created.
    System.Threading.Timer TimerItem =
        new System.Threading.Timer(TimerDelegate, StateObj, 2000, 2000);

    // Save a reference for Dispose.
    StateObj.TimerReference = TimerItem;  

    // Run for ten loops.
    while (StateObj.SomeValue < 10) 
    {
        // Wait one second.
        System.Threading.Thread.Sleep(1000);  
    }

    // Request Dispose of the timer object.
    StateObj.TimerCanceled = true;  
}

private void TimerTask(object StateObj)
{
    StateObjClass State = (StateObjClass)StateObj;
    // Use the interlocked class to increment the counter variable.
    System.Threading.Interlocked.Increment(ref State.SomeValue);
    System.Diagnostics.Debug.WriteLine("Launched new thread  " + DateTime.Now.ToString());
    if (State.TimerCanceled)    
    // Dispose Requested.
    {
        State.TimerReference.Dispose();
        System.Diagnostics.Debug.WriteLine("Done  " + DateTime.Now.ToString());
    }
}

スレッド タイマーは、コンソール アプリケーションを開発するときなど、System.Windows.Forms.Timer オブジェクトを使用できないときに特に有効です。

参照

関連項目

System.Threading

概念

マルチスレッド アプリケーション (C# および Visual Basic)