// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Threading.Tasks; using CoCoL; using Duplicati.Library.Utility; namespace Duplicati.GUI.TrayIcon; /// /// A class that delays the execution of actions. /// public class ProcessBasedActionDelay : IDisposable { /// /// The channel that sends the delayed actions, buffer avoids deadlocks if multiple events are queued before starting. /// private readonly IChannel m_inboundActionChannel = Channel.Create(name: "UI Action", buffersize: 500); /// /// The channel that sends the start signal. /// private readonly IChannel m_initializedChannel = Channel.Create(name: "UI Initializer"); /// /// Reference to the task running /// private readonly Task m_task; /// /// Initializes a new instance of the class. /// public ProcessBasedActionDelay() { m_task = RunProcessor(m_inboundActionChannel.AsReadOnly(), m_initializedChannel.AsReadOnly()); } /// /// Runs the processor process, which pauses until a ready signal is received. /// /// The channel with actions to be delayed. /// The channel that sends the start signal. /// The task running the processor process. private static Task RunProcessor(IReadChannelEnd inboundChannel, IReadChannelEnd initializedChannel) => AutomationExtensions.RunTask(new { inboundChannel, initializedChannel }, async (self) => { // Wait for initialization await self.initializedChannel.ReadAsync(); while (true) { var action = await self.inboundChannel.ReadAsync(); action(); } }); /// /// Adds a new task to the processor /// /// The action to execute public void ExecuteAction(Action action) { // Note: WriteNoWait() is used to avoid waiting for the action to be read, // as this would cause deadlocks if called from within the processor. // The buffer size should be sufficient to allow for a reasonable number of actions to be queued. var task = m_inboundActionChannel.WriteAsync(action); // Observe if the channel is full or retired Task.WaitAny(task, Task.Delay(500)); if (task.IsCompleted) task.Await(); } /// /// Signals the start of the processor /// public void SignalStart() => m_initializedChannel.TryWrite(true); /// /// Disposes the object /// public void Dispose() { m_inboundActionChannel.RetireAsync(true).Await(); m_initializedChannel.RetireAsync(true).Await(); } }