Gorgon
Show / Hide Table of Contents

Class GorgonTimerMultimedia

An implementation of the IGorgonTimer interface.

Inheritance
object
GorgonTimerMultimedia
Implements
IGorgonTimer
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
Namespace: Gorgon.Timing
Assembly: Gorgon.Windows.dll
Syntax
public sealed class GorgonTimerMultimedia : IGorgonTimer
Remarks

This implementation uses the Windows Multimedia timer. It fairly stable across all hardware and provides measurement of time down to 1 millisecond.

caution

Known issues

There are a few caveats when using this timer:
  • This timer wraps around to 0 every 2^32 milliseconds (~49.71 days), and can cause issues in code relying on this type of timer. Gorgon will do its best to ensure this is not a problem.
  • It has a default precision of 5 milliseconds, but Gorgon will attempt to change the resolution to 1 millisecond when using the GorgonApplication class.
The second bullet point is important, since it will change the timer frequency not only for the application, but across the system. This may cause weird issues with other applications relying on this timer. Also, since only the GorgonApplication class manages the period for the timer automatically, it'll be up to the user to set and reset the period frequency via the BeginTiming(int) and EndTiming() static methods on this class if the GorgonApplication class is not used in your application. It is vitally important that the EndTiming() method is called when the BeginTiming(int) method is used. Failure to do so can leave the operating system timing in a state where the task scheduler switches tasks more often and the power saving may not trigger properly.

Properties

| Edit this page View Source

Days

Property to return the number of days elapsed since the timer was started.

Declaration
public double Days { get; }
Property Value
Type Description
double
| Edit this page View Source

Hours

Property to return the number of hours elapsed since the timer was started.

Declaration
public double Hours { get; }
Property Value
Type Description
double
| Edit this page View Source

IsHighResolution

Property to return whether this timer has a resolution of less than 1 millisecond or not.

Declaration
public bool IsHighResolution { get; }
Property Value
Type Description
bool
| Edit this page View Source

Microseconds

Property to return the number of microseconds elapsed since the timer was started.

Declaration
public double Microseconds { get; }
Property Value
Type Description
double
Remarks

For this type of timer, this value is pretty much meaningless as the best precision of the timer is 1 millisecond at best (assuming BeginTiming(int) was called).

| Edit this page View Source

Milliseconds

Property to return the number of milliseconds elapsed since the timer was started.

Declaration
public double Milliseconds { get; }
Property Value
Type Description
double
| Edit this page View Source

Minutes

Property to return the number of minutes elapsed since the timer was started.

Declaration
public double Minutes { get; }
Property Value
Type Description
double
| Edit this page View Source

Seconds

Property to return the number of seconds elapsed since the timer was started.

Declaration
public double Seconds { get; }
Property Value
Type Description
double
| Edit this page View Source

Ticks

Property to return the number of ticks since the timer was started.

Declaration
public long Ticks { get; }
Property Value
Type Description
long

Methods

| Edit this page View Source

BeginTiming(int)

Function to set the timing period for the timer.

Declaration
public static void BeginTiming(int period = 1)
Parameters
Type Name Description
int period

[Optional] Minimum timer resolution, in milliseconds, for the application or device driver. A lower value specifies a higher (more accurate) resolution..

Remarks

This will set the timing period for calls to the Win32 timeGetTime API function so that the resolution for the timer can be adjusted for maximum performance.

Calls to this method must be paired with the EndTiming() static method, otherwise the system will be left in a state where the task scheduler switches tasks more often and the power saving may not trigger properly.

Exceptions
Type Condition
Win32Exception

Thrown when the period value is out of range.

| Edit this page View Source

EndTiming()

Function to end the timing period for the timer.

Declaration
public static void EndTiming()
Remarks

This will end the current timing period for calls to the Win32 timeGetTime API function.

Calls to this method must be paired with the BeginTiming(int) static method, otherwise the system will be left in a state where the task scheduler switches tasks more often and the power saving may not trigger properly. If BeginTiming(int) was never called, then this method will do nothing.

Exceptions
Type Condition
Win32Exception

Thrown when the last known period value is out of range.

| Edit this page View Source

Reset()

Function to reset the timer.

Declaration
public void Reset()
Exceptions
Type Condition
Win32Exception

Thrown when timer information cannot be retrieved from the operating system.

Implements

IGorgonTimer

Extension Methods

GorgonDebugExtensions.ValidateObject<T>(T, string)
GorgonNullExtensions.AsNullable<T>(object)
GorgonNullExtensions.IfNull<T>(object, T)
GorgonNullExtensions.IsNull(object)
  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author