Class ThreadThrottler
public class ThreadThrottler
- Inheritance
-
ThreadThrottler
Constructors
ThreadThrottler()
Create an instance of this class without throttling and defaulting to Standard mode. See SetToPreciseAuto() and SetToPreciseManual(long) to set it to other modes.
public ThreadThrottler()
ThreadThrottler(int)
Create an instance of this class set to Standard mode. See SetToPreciseAuto() and SetToPreciseManual(long) to set it to other modes.
public ThreadThrottler(int frequencyMax)
Parameters
frequencyMax
intThe maximum frequency this object allows
ThreadThrottler(TimeSpan)
Create an instance of this class set to Standard mode. See SetToPreciseAuto() and SetToPreciseManual(long) to set it to other modes.
public ThreadThrottler(TimeSpan minimumElapsedTimeParam)
Parameters
minimumElapsedTimeParam
TimeSpanMinimum time allowed between each call
Properties
MinimumElapsedTime
Set this to zero to disable throttling. Minimum amount of time allowed between each 'update'. Conversion is lossy, getting this value back might not return the same value you set it to.
public TimeSpan MinimumElapsedTime { get; set; }
Property Value
Remarks
See Throttle(out long)'s summary for an idea of how this property is used.
Type
The type of throttler used
public ThreadThrottler.ThrottlerType Type { get; }
Property Value
Methods
SetMaxFrequency(int)
Thread will be blocked to stay within the given amount of 'updates' per second.
public void SetMaxFrequency(int frequencyMax)
Parameters
frequencyMax
int
Remarks
It effectively transforms the given parameter from frame per second to the internal closest second per frame equivalent for it.
SetToPreciseAuto()
Most of the timings will be perfectly precise to the system timer at the cost of higher CPU usage.
If you aren't sure, use SetToStandard() instead.public void SetToPreciseAuto()
Remarks
This mode uses and automatically scales a spinwait window based on system responsiveness to find the right balance between Thread.Sleep calls and spinwaiting.
SetToPreciseManual(long)
Depending on the provided value, timings will be perfectly precise to the system timer at the cost of higher CPU usage.
If you aren't sure, use SetToStandard() instead.public void SetToPreciseManual(long spinwaitWindowParam)
Parameters
spinwaitWindowParam
long
Remarks
This mode uses the given value as the duration of the spinwait window. The format of this value is based on Stopwatch.Frequency
SetToStandard()
Saves CPU cycles while waiting, this one is the least precise mode but the lightest.
If you aren't sure, use this one !public void SetToStandard()
Throttle(out double)
Forces the thread to sleep when the time elapsed since last call is lower than MinimumElapsedTime, it will sleep for the time remaining to reach MinimumElapsedTime.
Use this function inside a loop when you want to lock it to a specific rate.public bool Throttle(out double elapsedInSeconds)
Parameters
elapsedInSeconds
doubleThe time since the last call in seconds, returns a value close to MinimumElapsedTime, use this value as your delta time.
Returns
- bool
True
if this class had to throttle,false
otherwise
Throttle(out long)
Forces the thread to sleep when the time elapsed since last call is lower than MinimumElapsedTime, it will sleep for the time remaining to reach that value.
Use this function inside a loop when you want to lock it to a specific rate.public bool Throttle(out long elapsedInSwFreq)
Parameters
elapsedInSwFreq
longThe time since the last call in Stopwatch.Frequency returns a value close to MinimumElapsedTime, use this value as your delta time.
Returns
- bool
True
if this class had to throttle,false
otherwise
Throttle(out TimeSpan)
Forces the thread to sleep when the time elapsed since last call is lower than MinimumElapsedTime, it will sleep for the time remaining to reach MinimumElapsedTime.
Use this function inside a loop when you want to lock it to a specific rate.public bool Throttle(out TimeSpan elapsedTimeSpan)
Parameters
elapsedTimeSpan
TimeSpanThe time since the last call, returns a value close to MinimumElapsedTime, use this value as your delta time.
Returns
- bool
True
if this class had to throttle,false
otherwise