Which C# Timer?
.net, c#, timer
Solution
Don't use `System.Windows.Forms.Timer` - This one will run the delegate on a form's UI thread, which is probably not what you want.
`System.Timers.Timer` derives from `System.ComponentModel.Component`, and so its geared towards a design surface.
`System.Threading.Timer` is best for background tasks on a thread pool.
`System.Threading.Timer` satisfies all your requirements (assuming you're trying to run delegates on the threadpool.
public void MyCallback(object o) { ... }
int timeToStart = 1000;
int period = 2000;
//fire the delegate after 1 second, and every 2 seconds from then on
Timer timer = new Timer(MyCallback, null, timeToStart, period);
//pause
timer.Change(Timeout.Infinite, Timeout.Infinite);
//resume
timer.Change(timeToStart, period);
//stop
timer.Dispose();
Problem
I'm writing a class that will include a timer (which crucially may not be initialized at 0, it may start already elapsed), and the class will include methods to Start, Pause, Resume and Stop/Complete. I'm aware of a number of timers in C# that I can use, i.e. System.Timers.Timer, however I'm not sure if this one lets me start the timer at a predefined elapsed figure. What is the best for this scenario?