Timing.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. namespace PaintDotNet.SystemLayer
  2. {
  3. /// <summary>
  4. /// Methods for keeping track of time in a high precision manner.
  5. /// </summary>
  6. /// <remarks>
  7. /// This class provides precision and accuracy of 1 millisecond.
  8. /// </remarks>
  9. public sealed class Timing
  10. {
  11. private ulong countsPerMs;
  12. private double countsPerMsDouble;
  13. private ulong birthTick;
  14. /// <summary>
  15. /// The number of milliseconds that elapsed between system startup
  16. /// and creation of this instance of Timing.
  17. /// </summary>
  18. public ulong BirthTick
  19. {
  20. get
  21. {
  22. return birthTick;
  23. }
  24. }
  25. /// <summary>
  26. /// Returns the number of milliseconds that have elapsed since
  27. /// system startup.
  28. /// </summary>
  29. public ulong GetTickCount()
  30. {
  31. ulong tick;
  32. SafeNativeMethods.QueryPerformanceCounter(out tick);
  33. return tick / countsPerMs;
  34. }
  35. /// <summary>
  36. /// Returns the number of milliseconds that have elapsed since
  37. /// system startup.
  38. /// </summary>
  39. public double GetTickCountDouble()
  40. {
  41. ulong tick;
  42. SafeNativeMethods.QueryPerformanceCounter(out tick);
  43. return (double)tick / countsPerMsDouble;
  44. }
  45. /// <summary>
  46. /// Constructs an instance of the Timing class.
  47. /// </summary>
  48. public Timing()
  49. {
  50. ulong frequency;
  51. if (!SafeNativeMethods.QueryPerformanceFrequency(out frequency))
  52. {
  53. NativeMethods.ThrowOnWin32Error("QueryPerformanceFrequency returned false");
  54. }
  55. countsPerMs = frequency / 1000;
  56. countsPerMsDouble = (double)frequency / 1000.0;
  57. birthTick = GetTickCount();
  58. }
  59. }
  60. }