Make a .net System.Timers.Timer fire immediately

Viewed 17557

I am using a Timer to run an event periodically on a reasonably long interval (2 minutes). This is working fine. However I would like the event to fire immediately when the timer is created (instead of waiting 2 minutes).

Note that I can't do this just by calling the method, since it takes some time to run and would block the application. I need the timer to fire as normal and run the event in a separate thread.

The best way I can think of doing this at the moment is subclassing the timer and creating a TriggerManually method that would do something like this:

  • Turn auto reset off
  • Set the interval to 1ms
  • Enable the timer

This would trigger the elapsed event straight away, and I could put all the settings back to normal.

Seems a bit roundabout though. Is there a better way to do it?

3 Answers
Related