Timer Question

07/19/2008 20:35 kinshi88#1
I have a question about Timers.
Well, really a question of "Am I doing this right?" =P

So, its the LOTF source, and this code is in General.cs.

So I have a Timer, and I'll show you what I have.

Code:
public static System.Timers.Timer Timer;
Code:
Timer = new System.Timers.Timer();
Timer.Interval = 300000;
Timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
Timer.Start();
Code:
public static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
    //STUFF HAPPENS
}

Is this the proper way to set up a timer? Or would I need some more coding. Thanks!


Also, how are the Intervals set up? like, how would I do 15 seconds for example?
07/19/2008 21:03 InfamousNoone#2
Intervals are in milliseconds;
1 second = 1 000 milliseconds
So 15s in milliseconds = 15 * 1000 = 15000

Personally I think the System.Timers.Timer sucks, I prefer the System.Threading.Timer, it's faster (run-time) and more simplistic.
07/19/2008 21:16 kinshi88#3
So to change to System.Threading.Timer, I would just change Timers to Threading right?

And if you could, whats the difference between Timers and Threading?


So to get say 15 minutes, I would go 15 x 60 x 1000 right?
07/19/2008 23:21 InfamousNoone#4
Yep, thats all you gotta do (for 15 minutes). The difference between the timer.timer and threading.timer is the way the "underlying" code works (the code Microsoft wrote). The threading.timer run runs faster because it is doing less work and setting up less information the the timer.timer.

Though I wrote my own timer class to process handling, I've rewrote a lot of Microsoft's classes just because most of the code they have is usually bulky and shit.
07/20/2008 23:28 kinshi88#5
Awesome, thanks.

Oh, alright, hey thanks a lot for the explainations!
So threading.timer would take less CPU and stuff right? Awesome, thank you!
07/21/2008 03:24 InfamousNoone#6
Yeah, if your intrested in learning how to make the most low-level efficient timer, look up "WM_TIMER" for "SendMessage"/"PostMessage"