// created on 07/13/2005 at 17:06 using System; using System.Runtime.InteropServices; using System.ComponentModel; using System.Threading; public class GameTimer{ //Import the APIs we need. [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceCounter(out long lpPerformanceCount); [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency(out long lpFrequency); public long last; public long now; private long freq; // Constructor public GameTimer(){ if (QueryPerformanceFrequency(out freq) == false){ throw new Win32Exception(); // timer not supported }else{ QueryPerformanceCounter(out last); QueryPerformanceCounter(out now); } } // Restarts the timer (starts timing from now). public void Mark(){ QueryPerformanceCounter(out last); } // Return the time since last Mark(). public double GetElapsedTime(){ QueryPerformanceCounter(out now); return ((double)(now - last) / (double)freq); } }