CancellationTokenSource需要建议

时间:2015-12-06 09:37:22

标签: c# .net winforms

我有一个像这样的班级工人:

public class Worker
{
    private List<SomeObject> _someObjectList = null;

    public Worker(SomeObject someObjectList)
    {
        _someObjectList = someObjectList;
    }

    public void Run(CancellationToken cancellationToken)
    {
        // Some time-consuming operation here
        foreach(var elem in _someObjectList)
        {
            cancellationToken.ThrowIfCancellationRequested();
            elem.DoSomethingLong();
        }
    }
}

我在工作中使用的一个表单:

public partial class SomeForm : Form
{
    private Worker _worker = null;

    public SomeForm(Worker worker)
    {
        InitializeComponent();

        _worker = worker;
    }

    async void RunButtonClick(object sender, EventArgs e)
    {
        // I have a way to cancel worker from MyForm but 
        //  I would like to be able to cancel it directly from Worker 
        //   so object would be intuitive.
        var tokenSource = new CancellationTokenSource();
        var task = Task.Factory.StartNew(() => _worker.Run(tokenSource.Token), tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        await task;     
    }
}

我需要一个更明确的解决方案来取消工人。像这样:

_worker.Cancel();

我还想像这样暂停和恢复工作人员:

_worker.Pause();
_worker.Resume();

因为我在Worker类之外实例化CancellationTokenSource,所以我看不到实现自己的Cancel方法。我已经用这样的CancellationToken实现了Pause和Resume(我觉得这是一个非常糟糕的想法,但它确实有效):

public class Worker
{
    private List<SomeObject> _someObjectList = null;
    private CancellationTokenSource _pauseToken = null;

    public bool Paused { get; private set; }

    public Worker(SomeObject someObjectList)
    {
        _someObjectList = someObjectList;
    }

    public void Run(CancellationToken cancellationToken)
    {
        // Some time-consuming operation here
        foreach(var elem in _someObjectList)
        {
            if(Paused)
                _pauseToken.Token.WaitHandle.WaitOne(Timeout.Infinite);

            cancellationToken.ThrowIfCancellationRequested();
            elem.DoSomethingLong();
        }
    }

    public void Pause()
    {
        if(!Paused)
        {
            // For pausing and resuming...
            _pauseToken = new CancellationTokenSource();

            Paused = true;

        }
    }

    public void Resume()
    {
        if(Paused && _pauseToken != null)
        {
            _pauseToken.Cancel();

            Paused = false;
        }
    }
}

我需要有关如何以最合适的方式实现Cancel方法和Resume / Pause方法的建议。

1 个答案:

答案 0 :(得分:1)

如果要将取消行为封装在Worker类中,显然必须在适当的时间在适当的对象上调用CancellationTokenSource.Cancel()。有两种明显的方法可以实现这一点:

  1. CancellationTokenSource对象嵌入Worker类本身。
  2. 提供一种回调机制,通过该机制调用Worker.Cancel()方法时,它会将实际操作委托给其他类。
  3. 第二种方法对我来说似乎是任意的错综复杂,而第一种方法似乎很适合你希望自己暴露Cancel()方法的类。


    至于暂停和恢复,我同意你的问题下面的评论,建议使用CancellationTokenSource是为此目的滥用该类型。也就是说,在现代C#代码中,没有理由使用ManualResetEvent之类的内容以及随之而来的伴随内容代码。

    相反,您可以将Run()方法实施为async,并在await可用时将其TaskCompletionSource。然后它将有公共方法Pause()Resume(),其中Pause()方法将创建TaskCompletionSource对象,而Resume()方法将设置结果对象

    这样做,您可以正常实现Run()方法,无需额外的工作来编写管家代码以允许方法暂停和恢复。编译器将为您生成所有代码,使用await语句作为方法在暂停时可以返回的位置,然后再继续执行。

    替代方法是自己编写所有管家代码(容易出错),或者甚至不从Run()方法返回,而是直接阻塞线程直到操作恢复(这是不必要的当你等待用户释放它时绑定一个帖子。)

    如果您只有一个任务和一个非常简单的用户场景,这可能有点过分。阻止线程可能就足够了。但是如果你有更复杂的场景,这种方法将完全支持这些方法,同时仍然提供最有效的线程池使用。


    这是一个简短的代码示例,演示了我上面描述的技术:

    <强> Worker.cs

    class Worker
    {
        private static readonly TimeSpan _ktotalDuration = TimeSpan.FromSeconds(5);
        private const int _kintervalCount = 20;
    
        public bool IsPaused { get { return _pauseCompletionSource != null; } }
        public event EventHandler IsPausedChanged;
    
        private readonly object _lock = new object();
        private CancellationTokenSource _cancelSource;
        private volatile TaskCompletionSource<object> _pauseCompletionSource;
    
        public async Task Run(IProgress<int> progress)
        {
            _cancelSource = new CancellationTokenSource();
    
            TimeSpan sleepDuration = TimeSpan.FromTicks(_ktotalDuration.Ticks / _kintervalCount);
    
            for (int i = 0; i < 100; i += (100 / _kintervalCount))
            {
                progress.Report(i);
                Thread.Sleep(sleepDuration);
                _cancelSource.Token.ThrowIfCancellationRequested();
    
                TaskCompletionSource<object> pauseCompletionSource;
    
                lock (_lock)
                {
                    pauseCompletionSource = _pauseCompletionSource;
                }
    
                if (pauseCompletionSource != null)
                {
                    RaiseEvent(IsPausedChanged);
    
                    try
                    {
                        await pauseCompletionSource.Task;
                    }
                    finally
                    {
                        lock (_lock)
                        {
                            _pauseCompletionSource = null;
                        }
                        RaiseEvent(IsPausedChanged);
                    }
                }
            }
    
            progress.Report(100);
    
            lock (_lock)
            {
                _cancelSource.Dispose();
                _cancelSource = null;
    
                // Just in case pausing lost the race with cancelling or finishing
                _pauseCompletionSource = null;
            }
        }
    
        public void Cancel()
        {
            lock (_lock)
            {
                if (_cancelSource != null)
                {
                    if (_pauseCompletionSource == null)
                    {
                        _cancelSource.Cancel();
                    }
                    else
                    {
                        _pauseCompletionSource.SetCanceled();
                    }
                }
            }
        }
    
        public void Pause()
        {
            lock (_lock)
            {
                if (_pauseCompletionSource == null)
                {
                    _pauseCompletionSource = new TaskCompletionSource<object>();
                }
            }
        }
    
        public void Resume()
        {
            lock (_lock)
            {
                if (_pauseCompletionSource != null)
                {
                    _pauseCompletionSource.SetResult(null);
                }
            }
        }
    
        private void RaiseEvent(EventHandler handler)
        {
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
    

    <强> Form1.cs的

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private Worker _worker;
    
        private async void button1_Click(object sender, EventArgs e)
        {
            Progress<int> progress = new Progress<int>(i => progressBar1.Value = i);
            _worker = new Worker();
    
            _worker.IsPausedChanged += (sender1, e1) =>
            {
                Invoke((Action)(() =>
                {
                    button3.Enabled = !_worker.IsPaused;
                    button4.Enabled = _worker.IsPaused;
                }));
            };
    
            button1.Enabled = false;
            button2.Enabled = button3.Enabled = true;
    
            try
            {
                await Task.Run(() => _worker.Run(progress));
    
                // let the progress bar catch up before we clear it
                await Task.Delay(1000);
            }
            catch (OperationCanceledException)
            {
                MessageBox.Show("Operation was cancelled");
            }
    
            progressBar1.Value = 0;
            button2.Enabled = button3.Enabled = button4.Enabled = false;
            button1.Enabled = true;
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            _worker.Cancel();
        }
    
        private void button3_Click(object sender, EventArgs e)
        {
            _worker.Pause();
        }
    
        private void button4_Click(object sender, EventArgs e)
        {
            _worker.Resume();
        }
    }
    

    <强> Form1.Designer.cs

    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        #region Windows Form Designer generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // progressBar1
            // 
            this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.progressBar1.Location = new System.Drawing.Point(12, 42);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(427, 23);
            this.progressBar1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 13);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Start";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Enabled = false;
            this.button2.Location = new System.Drawing.Point(94, 13);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "Cancel";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Enabled = false;
            this.button3.Location = new System.Drawing.Point(175, 13);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 3;
            this.button3.Text = "Pause";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Enabled = false;
            this.button4.Location = new System.Drawing.Point(256, 13);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 4;
            this.button4.Text = "Resume";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(451, 287);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.progressBar1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
    
        }
    
        #endregion
    
        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
    }