// Copyright (C) 2015, The Duplicati Team // http://www.duplicati.com, info@duplicati.com // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; using System.Threading.Tasks; namespace Duplicati.Library.Main.Operation.Common { /// /// Interface for async access to the current control state /// public interface ITaskReader { /// /// Processing tasks can regularly await this, /// which will pause the task or stop it if required. /// The return value is true if the program should continue and false if a stop is requested /// Task ProgressAsync { get; } /// /// Gets the transfer progress async control. /// The transfer handler should await this instead of the event. /// Task TransferProgressAsync { get; } } /// /// Interface for controlling the progress /// public interface ITaskCommander { /// /// Requests that progress should be paused /// /// If set to true transfer are also suspended. void Pause(bool alsoTransfers = false); /// /// Resumes running a paused process /// void Resume(); /// /// Requests that the progress should be stopped in an orderly manner, /// which allows current transfers to be completed. /// /// If set to true active transfer are also stopped. void Stop(bool alsoTransfers = false); /// /// Terminates the progress without allowing a flush /// void Terminate(); } /// /// Implementation of the task control /// public class TaskControl : ITaskReader, ITaskCommander, IDisposable { /// /// Internal state tracking to avoid invalid requests /// private enum State { /// /// The task is running /// Active, /// /// The task is paused /// Paused, /// /// The task is stopped /// Stopped, /// /// The task is terminated /// Terminated } /// /// Internal control state for progress event /// private TaskCompletionSource m_progress; /// /// Internal control state for the transfer event /// private TaskCompletionSource m_transfer; /// /// The control lock instance /// private readonly object m_lock = new object(); /// /// The current progress state /// private State m_progressstate = State.Paused; /// /// The current transfer state /// private State m_transferstate = State.Paused; /// /// Processing tasks can regularly await this, /// which will pause the task or stop it if required. /// The return value is true if the program should continue and false if a stop is requested /// public Task ProgressAsync { get { return m_progress.Task; } } /// /// Gets the transfer progress async control. /// The transfer handler should await this instead of the event. /// public Task TransferProgressAsync { get { return m_transfer.Task; } } /// /// Initializes a new instance of the class. /// public TaskControl() { m_progress = new TaskCompletionSource(); m_transfer = new TaskCompletionSource(); Resume(); } /// /// Resumes running a paused process /// public void Resume() { lock(m_lock) { if (m_progressstate == State.Paused) { m_progress.SetResult(true); m_progressstate = State.Active; } if (m_transferstate == State.Paused) { m_transfer.SetResult(true); m_transferstate = State.Active; } } } /// /// Requests that progress should be paused /// /// If set to true also transfers. public void Pause(bool alsoTransfers = false) { lock(m_lock) { if (m_progressstate == State.Active) { m_progress = new TaskCompletionSource(); m_progressstate = State.Paused; } if (alsoTransfers && m_transferstate == State.Active) { m_transfer = new TaskCompletionSource(); m_transferstate = State.Paused; } } } /// /// Requests that the progress should be stopped in an orderly manner, /// which allows current transfers to be completed. /// /// If set to true also transfers. public void Stop(bool alsoTransfers = false) { lock(m_lock) { if (m_progressstate == State.Active || m_progressstate == State.Paused) { if (m_progressstate != State.Paused) m_progress = new TaskCompletionSource(); m_progress.SetResult(false); m_progressstate = State.Stopped; } if (alsoTransfers && (m_transferstate == State.Active || m_transferstate == State.Paused)) { if (m_transferstate != State.Paused) m_transfer = new TaskCompletionSource(); m_transfer.SetResult(false); m_transferstate = State.Stopped; } } } /// /// Terminates the progress without allowing a flush /// public void Terminate() { lock(m_lock) { if (m_progressstate != State.Terminated) { if (m_progressstate != State.Paused) m_progress = new TaskCompletionSource(); m_progress.SetCanceled(); m_progressstate = State.Terminated; } if (m_transferstate != State.Terminated) { if (m_transferstate != State.Paused) m_transfer = new TaskCompletionSource(); m_transfer.SetCanceled(); m_transferstate = State.Terminated; } } } /// /// Releases all resource used by the object. /// /// Call when you are finished using the /// . The method leaves /// the in an unusable state. After calling /// , you must release all references to the /// so the garbage collector can reclaim the /// memory that the was occupying. public void Dispose() { Terminate(); } } }