Click here to Skip to main content
15,885,216 members
Articles / General Programming / Threads

Smart Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.96/5 (314 votes)
27 Aug 2012Ms-PL40 min read 2.2M   29.1K   1.1K  
A .NET Thread Pool fully implemented in C# with many features.
using System;

namespace Amib.Threading.Internal
{
    /// <summary>
    /// Stopwatch class
    /// Used with WindowsCE and Silverlight which don't have Stopwatch
    /// </summary>
    internal class Stopwatch
    {
        private long _elapsed;
        private bool _isRunning;
        private long _startTimeStamp;

        public Stopwatch()
        {
            Reset();
        }

        private long GetElapsedDateTimeTicks()
        {
            long rawElapsedTicks = GetRawElapsedTicks();
            return rawElapsedTicks;
        }

        private long GetRawElapsedTicks()
        {
            long elapsed = _elapsed;
            if (_isRunning)
            {
                long ticks = GetTimestamp() - _startTimeStamp;
                elapsed += ticks;
            }
            return elapsed;
        }

        public static long GetTimestamp()
        {
            return DateTime.UtcNow.Ticks;
        }

        public void Reset()
        {
           _elapsed = 0L;
           _isRunning = false;
           _startTimeStamp = 0L;
        }

        public void Start()
        {
            if (!_isRunning)
            {
                _startTimeStamp = GetTimestamp();
                _isRunning = true;
            }
        }

        public static Stopwatch StartNew()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            return stopwatch;
        }

        public void Stop()
        {
            if (_isRunning)
            {
                long ticks = GetTimestamp() - _startTimeStamp;
                _elapsed += ticks;
                _isRunning = false;
            }
        }

        // Properties
        public TimeSpan Elapsed
        {
            get
            {
                return new TimeSpan(GetElapsedDateTimeTicks());
            }
        }

        public long ElapsedMilliseconds
        {
            get
            {
                return (GetElapsedDateTimeTicks() / 0x2710L);
            }
        }

        public long ElapsedTicks
        {
            get
            {
                return GetRawElapsedTicks();
            }
        }

        public bool IsRunning
        {
            get
            {
                return _isRunning;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Israel Israel
B.Sc. in Computer Science.
Works as Software Developer.

Comments and Discussions