// ReSharper disable MemberCanBePrivate.Global using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace RGB.NET.Core { /// /// /// Represents an /// public sealed class ManualUpdateTrigger : AbstractUpdateTrigger { #region Properties & Fields private AutoResetEvent _mutex = new(false); private Task? UpdateTask { get; set; } private CancellationTokenSource? UpdateTokenSource { get; set; } private CancellationToken UpdateToken { get; set; } /// /// Gets the time it took the last update-loop cycle to run. /// public override double LastUpdateTime { get; protected set; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// A value indicating if the trigger should automatically right after construction. public ManualUpdateTrigger() { Start(); } #endregion #region Methods /// /// Starts the trigger if needed, causing it to performing updates. /// public override void Start() { if (UpdateTask == null) { UpdateTokenSource?.Dispose(); UpdateTokenSource = new CancellationTokenSource(); UpdateTask = Task.Factory.StartNew(UpdateLoop, (UpdateToken = UpdateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default); } } /// /// Stops the trigger if running, causing it to stop performing updates. /// private void Stop() { if (UpdateTask != null) { UpdateTokenSource?.Cancel(); // ReSharper disable once MethodSupportsCancellation UpdateTask.Wait(); UpdateTask.Dispose(); UpdateTask = null; } } public void TriggerUpdate() => _mutex.Set(); private void UpdateLoop() { OnStartup(); while (!UpdateToken.IsCancellationRequested) { if (_mutex.WaitOne(100)) { long preUpdateTicks = Stopwatch.GetTimestamp(); OnUpdate(); LastUpdateTime = ((Stopwatch.GetTimestamp() - preUpdateTicks) / 10000.0); } } } /// public override void Dispose() => Stop(); #endregion } }