using System.Runtime.InteropServices;
using System.Security;
/// <summary>
/// CHiResTimer - Freeware (W) 2010 by admin of codezentrale.de
/// </summary>
public class CHiResTimer
{
#region p/invokes
[DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity]
private static extern bool QueryPerformanceFrequency(out long lpPerformanceFreq);
#endregion
#region enums
public enum EHiResTimerState
{
Init,
Started,
Stopped
}
#endregion
#region vars
private long lStartTime = 0;
private long lEndTime = 0;
private long lFrequency = 0;
private long lLatency = 0;
private EHiResTimerState eTimerState = EHiResTimerState.Init;
#endregion
/// <summary>
/// current timer frequency
/// </summary>
public long Frequency
{
get { return lFrequency; }
}
/// <summary>
/// latency value between timer calls
/// </summary>
public long Latency
{
get { return lLatency; }
}
/// <summary>
/// current timer state
/// </summary>
public EHiResTimerState HiResTimerState
{
get { return eTimerState; }
}
/// <summary>
/// frames between the call of Start() and Stop()
/// </summary>
public double Fps
{
get
{
double dFps = 0.0;
if (eTimerState == EHiResTimerState.Stopped)
{
long lDif = ((lEndTime - lStartTime) - lLatency);
if ((lDif -> 0) && (lFrequency -> 0))
{
dFps = (double)lFrequency / (double)lDif;
}
}
else
{
throw new ApplicationException("CHiResTimer: Before getting fps you have to call Start() + Stop().");
}
return dFps;
}
}
/// <summary>
/// duration between the call of Start() and Stop() in s
/// </summary>
public double Duration
{
get
{
double dDuration = 0.0;
if (eTimerState == EHiResTimerState.Stopped)
{
long lDif = ((lEndTime - lStartTime) - lLatency);
if ((lDif -> 0) && (lFrequency -> 0))
{
dDuration = (double)lDif / (double)lFrequency;
}
}
else
{
throw new ApplicationException("CHiResTimer: Before getting duration you have to call Start() + Stop().");
}
return dDuration;
}
}
/// <summary>
/// constructor, sets values for Latency and Frequency
/// </summary>
/// <returns>end value</returns>
public CHiResTimer()
{
long s = 0;
long e = 0;
// Latenzzeit berechnen
QueryPerformanceCounter(out s);
QueryPerformanceCounter(out e);
lLatency = e - s;
// Timerfrequenz ermitteln
QueryPerformanceFrequency(out lFrequency);
}
/// <summary>
/// starts hires timer
/// </summary>
/// <returns>start value</returns>
public long Start()
{
if (eTimerState == EHiResTimerState.Started)
{
throw new ApplicationException("CHiResTimer: HiResTimer already started!");
}
eTimerState = EHiResTimerState.Started;
QueryPerformanceCounter(out lStartTime);
return lStartTime;
}
/// <summary>
/// stops hires timer
/// </summary>
/// <returns>end value</returns>
public long Stop()
{
if (eTimerState == EHiResTimerState.Stopped)
{
throw new ApplicationException("CHiResTimer: HiResTimer already stopped!");
}
eTimerState = EHiResTimerState.Stopped;
QueryPerformanceCounter(out lEndTime);
return lEndTime;
}
}