Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Thread Timers

Last updated Jan 1, 2004.

The System.Threading.Timer class implements a simple lightweight timer that uses callback functions and is served by threadpool threads. This type of timer is not a component like the other two. It also has fewer features. It has the benefit, though, of not going through the event system, but rather making direct callbacks. When you create the timer, you specify the callback method, the time to wait before the first time the callback is executed, and the amount of time (period) between calls. You can't change the callback method once the timer has been created, but you can change the period or turn the timer off by calling the Change method.

You can pass information to the timer callback by creating a status object and passing a reference to that object to the constructor. This object is then passed to the timer callback where it can be modified.

The code below shows how you can use this type of timer in a Windows Forms program.

C#

// create the timer's state object
class TimerState
{
  public int counter = 0;
  public System.Threading.Timer tmr = null;
  public bool enabled = false;
}

private TimerState ts = new TimerState();

private void timer3Callback(object state)
{
  TimerState s = (TimerState)state;
  s.counter++;
  ShowLabel(label5, s.counter.ToString());
}

private void btnThreadTimer_Click(object sender, System.EventArgs e)
{
  if (ts.enabled)
  {
    // disable the timer
    ts.enabled = false;
    ts.tmr.Change(System.Threading.Timeout.Infinite,
      System.Threading.Timeout.Infinite);
    btnThreadTimer.Text = "Start";
  }
  else
  {
    if (ts.tmr == null)
      // first time through, create the timer
      ts.tmr = new System.Threading.Timer(
        new TimerCallback(timer3Callback), 
        ts, 1000, 1000);
    else
      // enable the timer
      ts.tmr.Change(1000, 1000);
    ts.enabled = true;
    btnThreadTimer.Text = "Stop";
  }
}

Visual Basic

' create the timer's state object
Class TimerState
  Public counter As Integer = 0
  Public tmr As System.Threading.Timer = Nothing
  Public enabled As Boolean = False
End Class

Private ts As TimerState = New TimerState

Private Sub timer3Callback(ByVal state As Object)
  Dim s As TimerState = DirectCast(state, TimerState)
  s.counter = s.counter + 1
  ShowLabel(label5, s.counter.ToString())
End Sub

Private Sub btnThreadTimer_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles btnThreadTimer.Click
  If ts.enabled Then
    ' disable the timer
    ts.enabled = False
    ts.tmr.Change(System.Threading.Timeout.Infinite, _
    System.Threading.Timeout.Infinite)
    btnThreadTimer.Text = "Start"
  Else
    If ts.tmr Is Nothing Then
      ' first time through, create the timer
      ts.tmr = New System.Threading.Timer( _
        New System.Threading.TimerCallback(AddressOf 
timer3Callback), _
        ts, 1000, 1000)

    Else
      ' enable the timer
      ts.tmr.Change(1000, 1000)
    End If
    ts.enabled = True
    btnThreadTimer.Text = "Stop"
  End If
End Sub

Although you could use a thread timer for this purpose, the System.Timer or Windows.Forms.Timer is probably more appropriate. The primary use for thread timers is within threads, or where you don't want the overhead of going through the events subsystem.

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network