using System.Runtime.InteropServices;
using System.ComponentModel;
namespace Microsoft.Msagl.DebugHelpers {
#if REPORTING
///
/// support for native method calls
///
public class Timer {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
private long startTime;
private long stopTime;
private long freq;
///
/// ctor
///
public Timer() {
if (QueryPerformanceFrequency(out freq) == false) {
throw new Win32Exception(); // timer not supanchored
}
}
///
/// Start the timer
///
/// long - tick count
public long Start() {
QueryPerformanceCounter(out startTime);
return startTime;
}
///
/// Stop timer
///
/// long - tick count
public long Stop() {
QueryPerformanceCounter(out stopTime);
return stopTime;
}
///
/// Return the duration of the timer in seconds.
///
/// double - duration
public double Duration {
get {
return (double)(stopTime - startTime) / (double)freq;
}
}
///
/// Frequency of timer (no counts in one second on this machine)
///
///long - Frequency
public long Frequency {
get {
QueryPerformanceFrequency(out freq);
return freq;
}
}
}
#endif
}