Thread Timer

24 Aug

There are three timer controls in the .NET Framework:

  • A Windows-based timer, which is always in the Visual Studio Toolbox (System.Windows.Forms.Timer)
  • A server-based timer, which you can add to the Visual Studio Toolbox (System.Timers.Timer)
  • A thread timer, which is available programmatically (System.Threading.Timer)

If you are using Windows Forms and probably updating a UI, use System.Windows.Forms.Timer.

For server-based timer functionality, you might consider using System.Timers.Timer, which is also more accurate and has additional features.

The third one, System.Threading.Timer, uses a separate thread for it’s operation. If you need to do an asynchronous job periodically, it may be useful.

Using  System.Threading.Timer:

System.Threading.Timer timer;
timer = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 1000);

DoSomething() executes at specified intervals(1000 ms here). Note that DoSomething() does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system.

private void DoSomething(object obj)
{
//it executes every second
}

To enable/disable timer, use Change() method:

timer.Change(Timeout.Infinite, Timeout.Infinite); //disable
timer.Change(0, 1000); //enable

6 Responses to “Thread Timer”

  1. MC July 25, 2012 at 12:37 am #

    Thanks for the code.
    Instead of using:
    “System.Threading.Timer timer;
    timer = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 1000);”

    You can just use:
    new System.Threading.Timer(DoSomething, null, 0, 1000);

  2. isakavis July 26, 2012 at 6:30 pm #

    Simple precise exmple.. Excellent

  3. Farshad September 28, 2012 at 6:28 pm #

    Thank You very much It works perfect for me

  4. Ashoke Kumart February 4, 2013 at 8:58 am #

    I have the questions, i am using “System.Threading.Timer timer;
    timer = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 1000);”
    four times and myservice stucks on one place, and i could not found any exception or logging or an thing else, i could not trace that error,
    what i do in dosomething, let me explain, when i enter in dosomething function i change the timer(-1,-1) and then do my work, at the end i change it in +ve vlaue, and other time also have the same behaviour except one timer which do same things but in this timer i am setting the threadpool.max(25,25); All of this working fine at me, but when clients run, it complete the one cycle and then stucks the service. and nothing to do for hours… need helpfull deeds.
    so please help me to solve out this.
    Thanks

  5. Geoffrey hunter March 13, 2013 at 7:26 am #

    I had to declare the function “DoSomething()” as static before it would compile.

  6. Zinc February 12, 2014 at 9:03 pm #

    There seems to be an issue with System.Threading.Timers with regards to garbage collection–I set up a timer for every 5 minutes, it runs for about 20 mins or so and then stops. I’ve read suggestions to use GC.KeepAlive(timer) to fix this, but haven’t been able to make that work because the examples aren’t specific enough. Could use a complete example with the proper use of GC.Keepalive or whatever is necessary to keep the garbage collector at bay…

Leave a reply to Ashoke Kumart Cancel reply