Quote:
Originally Posted by pwerty
@pro4never
1 timer that goes over all clients and check for times and update them. But that would make delays for players. More clients, bigger gap ( if Im not wrong)
tho thread sounds more suitable for this (if coded right).
|
There's no real difference between timers and threads when it comes to purpose...
They both deal with delayed or timed actions.
Threads obviously server a better use in most 'constant' actions where threads are more often used in something that only happens periodically (many exceptions but that's how I think of it)
As for actual execution time though... when a timer executes its code it's running on a separate thread so there's no real differences there.
The only way you could 'load balance' the code would be to determine how long the code inside took to run and reduce the loop time by that amount.
EXAMPLE:
Code:
while (true)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
//RUN YOUR CODE
sw.Stop();
Thread.Sleep((int)Math.Max(10, 100 - sw.ElapsedMilliseconds));
}
Obviously a rather trash example but what I'm saying is that if you have a thread which for some odd reason takes you.... 50 ms to process all of (seems a large amount of processing per thread loop imo... but to each his own!) then you reduce the final thread sleep by 50.
I suggest the Math.Max simply because what happens if you want the thread to run every 100 ms but this loop it fucks up and takes 500 ms to process... you could end up with a negative number and I have no idea what a negative number would do to a thread.sleep.