#if SILVERLIGHT
using System;
namespace FarseerPhysics.Common
{
// Source: http://www.wiredprairie.us/blog/index.php/archives/723
///
/// An emulation for the Stopwatch class for Windows Phone and Silverlight.
///
public sealed class Stopwatch
{
private long _startTick;
private long _elapsed;
private bool _isRunning;
///
/// Creates a new instance of the class 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 class.
///
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