Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

Thread Timers (C# and Visual Basic)

The System.Threading.Timer class is useful for periodically running a task on a separate thread. For example, you could use a thread timer to check the status and integrity of a database or to back up critical files.

The following example starts a task every two seconds and uses a flag to initiate the Dispose method that stops the timer. This example posts status to the output window.

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());
    }
}

Thread timers are particularly useful when the System.Windows.Forms.Timer object is unavailable, such as when you are developing console applications.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.