Static methods are inherently not thread safe. With their help, they are not processed otherwise than instance methods. The difference is that you usually need to try to make them thread safe. (I cannot think of any static .NET BCL methods that are not thread safe.) Instance methods are often not thread safe because a typical template is to create an object and reuse it from a single thread, and if you need to use it from multiple threads, and coordination includes ensuring the safe use of the facility. In so many cases, this is more appropriate to do in the coordinating code than in the object itself. (Normally, you want entire sequences of operations to be efficiently atomic - something that cannot be done in an object.)
Your Timer class is most definitely not thread safe: two threads can easily tear data off one another, and there is nothing to stop the thread from using "stale" data when calculating the duration.
Instead, use Stopwatch - what is it for? Admittedly, if you want to use one instance of several threads, you need to take the usual steps to ensure security, but you will be in a much better position overall. Admittedly, Stopwatch far from perfect - see this question and comment below for more details - but this is at least what the type is for. (Who knows, this could be fixed for a while ...)
Jon Skeet Jul 07 '09 at 6:22 2009-07-07 06:22
source share