csharp/Alan-FGR/aelum/Farseer/Farseer%20Physics%20Engine%203.5/Common/Stopwatch.cs

Stopwatch.cs
#if SILVERLIGHT
using System;

namespace FarseerPhysics.Common
{
    // Source: http://www.wiredprairie.us/blog/index.php/archives/723

    /// 
    /// An emulation for the Stopwatch clast for Windows Phone and Silverlight.
    /// 
    public sealed clast Stopwatch
    {
        private long _startTick;
        private long _elapsed;
        private bool _isRunning;

        /// 
        /// Creates a new instance of the clast and starts the watch immediately.
        /// 
        /// An instance of Stopwatch, running.
        public static Stopwatch StartNew()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            return sw;
        }

        /// 
        /// Creates an instance of the Stopwatch clast.
        /// 
        public Stopwatch() { }

        /// 
        /// Completely resets and deactivates the timer.
        /// 
        public void Reset()
        {
            _elapsed = 0;
            _isRunning = false;
            _startTick = 0;
        }

        /// 
        /// Begins the timer.
        /// 
        public void Start()
        {
            if (!_isRunning)
            {
                _startTick = GetCurrentTicks();
                _isRunning = true;
            }
        }

        /// 
        /// Stops the current timer.
        /// 
        public void Stop()
        {
            if (_isRunning)
            {
                _elapsed += GetCurrentTicks() - _startTick;
                _isRunning = false;
            }
        }

        /// 
        /// Gets a value indicating whether the instance is currently recording.
        /// 
        public bool IsRunning
        {
            get { return _isRunning; }
        }

        /// 
        /// Gets the Elapsed time as a Timespan.
        /// 
        public TimeSpan Elapsed
        {
            get { return TimeSpan.FromMilliseconds(ElapsedMilliseconds); }
        }

        /// 
        /// Gets the Elapsed time as the total number of milliseconds.
        /// 
        public long ElapsedMilliseconds
        {
            get { return GetCurrentElapsedTicks() / TimeSpan.TicksPerMillisecond; }
        }

        /// 
        /// Gets the Elapsed time as the total number of ticks (which is faked
        /// as Silverlight doesn't have a way to get at the actual "Ticks")
        /// 
        public long ElapsedTicks
        {
            get { return GetCurrentElapsedTicks(); }
        }

        private long GetCurrentElapsedTicks()
        {
            return _elapsed + (IsRunning ? (GetCurrentTicks() - _startTick) : 0);
        }

        private long GetCurrentTicks()
        {
            // TickCount: Gets the number of milliseconds elapsed since the system started.
            return Environment.TickCount * TimeSpan.TicksPerMillisecond;
        }
    }
}
#endif