// ReSharper disable MemberCanBePrivate.Global using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace RGB.NET.Core { /// /// Represents an update-trigger used to update devices with a maximum update-rate. /// public class DeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger { #region Properties & Fields /// /// Gets or sets the timeout used by the blocking wait for data availability. /// public int Timeout { get; set; } = 100; /// /// Gets the update frequency used by the trigger if not limited by data shortage. /// public double UpdateFrequency { get; private set; } private double _maxUpdateRate; /// /// Gets or sets the maximum update rate of this trigger (is overwriten if the is smaller). /// <= 0 removes the limit. /// public double MaxUpdateRate { get => _maxUpdateRate; set { _maxUpdateRate = value; UpdateUpdateFrequency(); } } private double _updateRateHardLimit; /// /// Gets the hard limit of the update rate of this trigger. Updates will never perform faster then then this value if it's set. /// <= 0 removes the limit. /// public double UpdateRateHardLimit { get => _updateRateHardLimit; protected set { _updateRateHardLimit = value; UpdateUpdateFrequency(); } } protected AutoResetEvent HasDataEvent = new AutoResetEvent(false); protected bool IsRunning; protected Task UpdateTask; protected CancellationTokenSource UpdateTokenSource; protected CancellationToken UpdateToken; #endregion #region Constructors /// /// Initializes a new instance of the class. /// public DeviceUpdateTrigger() { } /// /// Initializes a new instance of the class. /// /// The hard limit of the update rate of this trigger. public DeviceUpdateTrigger(double updateRateHardLimit) { this.UpdateRateHardLimit = updateRateHardLimit; } #endregion #region Methods /// /// Starts the trigger. /// public void Start() { if (IsRunning) return; IsRunning = true; UpdateTokenSource?.Dispose(); UpdateTokenSource = new CancellationTokenSource(); UpdateTask = Task.Factory.StartNew(UpdateLoop, (UpdateToken = UpdateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default); } /// /// Stops the trigger. /// public async void Stop() { if (!IsRunning) return; IsRunning = false; UpdateTokenSource.Cancel(); await UpdateTask; UpdateTask.Dispose(); UpdateTask = null; } protected virtual void UpdateLoop() { OnStartup(); while (!UpdateToken.IsCancellationRequested) { if (HasDataEvent.WaitOne(Timeout)) { long preUpdateTicks = Stopwatch.GetTimestamp(); OnUpdate(); if (UpdateFrequency > 0) { double lastUpdateTime = ((Stopwatch.GetTimestamp() - preUpdateTicks) / 10000.0); int sleep = (int)((UpdateFrequency * 1000.0) - lastUpdateTime); if (sleep > 0) Thread.Sleep(sleep); } } } } /// public void TriggerHasData() => HasDataEvent.Set(); private void UpdateUpdateFrequency() { UpdateFrequency = MaxUpdateRate; if ((UpdateFrequency <= 0) || ((UpdateRateHardLimit > 0) && (UpdateRateHardLimit < UpdateFrequency))) UpdateFrequency = UpdateRateHardLimit; } /// public override void Dispose() => Stop(); #endregion } }