tags:

views:

1517

answers:

4

I have a static timer class which will be called by ANY webpage to calculate how long each page has taken to be constructed.

My question is are Static classes thread safe? In my example will concurrent users cause a problem with my start and stop times? e.g a different threads overwriting my start and stop values.

public static class Timer
    {
        private static DateTime _startTime;
        private static DateTime _stopTime;    

        /// <summary>
        /// Gets the amount of time taken in milliseconds
        /// </summary>
        /// <returns></returns>
        public static decimal Duration()
        {
            TimeSpan duration =  _stopTime - _startTime;
            return duration.Milliseconds;
        }

        public static void Start()
        {
            _startTime = DateTime.Now;
        }

        public static void Stop()
        {
            _stopTime = DateTime.Now;
        }

Should this class be a non-static class?

(This class will called from the asp.net masterpage.)

+1  A: 

Yes, you're right, the static members / accessors on this class will cause them to be overwritten by different users.

This is why you have instances and non-static members.

Tom
+10  A: 

Static methods aren't inherently thread-safe. They're treated no differently by the CLR than instance methods. The difference is that one should generally try to make them thread-safe. (I can't think of any .NET BCL static methods which aren't thread-safe.) Instance methods are often not thread-safe because the typical pattern is to create an object and use it repeatedly from one thread, and if it does have to be used from multiple threads, the co-ordination involved includes making sure that the object is used safely. In very many cases that's more appropriate to do in the co-ordinating code than in the object itself. (Usually you want to make whole sequences of operations effectively atomic - something which can't be done within the object.)

Your Timer class is most definitely not thread-safe: two threads can stomp on each other's data with ease, and there's nothing to stop a thread from using "stale" data when calculating the duration.

Use the Stopwatch class instead - that's what it's there for. Admittedly if you want to use one instance from multiple threads you'll need to take the normal steps to ensure safety, but you'll be in a much better position in general.

Jon Skeet
+3  A: 

Your timer class is definitely not thread-safe. You should create a normal class and instantiate it every time you need to measure the time:

Timer timer = new Timer();

timer.Start();
//...
timer.Stop();

decimal duration = timer.Duration();

Better still, there is a built-in .NET class that does exactly that:

Stopwatch sw = Stopwatch.StartNew();

sw.Stop();

TimeSpan duration = sw.Elapsed;
Philippe Leybaert
A: 

The class is Thread Resistent... Yes some resistence is there, HOW... Static data is stored in Stack not in Heap if it gets copied to Heap for new Object invocations then for that perticular invocation it will not get curropted by other threads.

Tell me if i am wrong.....

:)