[C#] System.Timers.Timer vs System.Threading.Timer
Haven't really looked into the timers until today. My previous method required checks of a integer every 100 or so milliseconds.
There are many more timers within the .NET CLR, but these two in particular have thread pooling (call backs) which is one one of my priorities. I am wondering if anyone has used both / have any experience with them, cause I've checked out both, and they seem to have the same functionality, except Threading.Timer doesn't support instances of safe threads and unable to add/remove listeners after instantiated. I plan on adding events (quantity varies depending on users).
Timers.Timer class (and examples): http://msdn.microsoft.com/en-us/libr...ers.timer.aspx
Threading.Timer class (and examples): http://msdn.microsoft.com/en-us/libr...ing.timer.aspx
Example of Timers.Timer:
Code:
static Timer timer;
static void Main()
{
// Append a new event. 1 Second interval.
AppendEvent(
delegate
{
for (int i = 0; i < 5; i++)
{
Console.Write(i);
Console.WriteLine();
}
}, 1000);
}
static void AppendEvent(ElapsedEventHandler event, double interval)
{
timer = new Timer(interval);
timer.Elapsed += new ElapsedEventHandler(event);
timer.Start();
}
Code:
static Timer timer;
static void Main()
{
AppendEvent(
delegate
{
Console.WriteLine(DateTime.Now);
timer.Dispose();
}, null, 1000, 0);
Console.WriteLine("lol");
Console.ReadLine();
}
static void AppendEvent(TimerCallback callBack, object state, int dueTime, int period)
{
timer = new Timer(callBack, state, dueTime, period);
}