gpt4 book ai didi

c# - CancellationTokenSource 需要建议

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:17 30 4
gpt4 key购买 nike

我有一个这样的 Worker 类:

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();
}
}
}

还有一个我使用 worker 的表单:

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 。像这样:

_worker.Cancel();

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

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

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

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 方法的建议。

最佳答案

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

  1. 嵌入 CancellationTokenSource Worker 中的对象类本身。
  2. 提供一个回调机制,当Worker.Cancel()方法被调用时,它将实际操作委托(delegate)给其他一些类。

第二种方法对我来说似乎是任意的令人费解,而第一种方法似乎很适合您希望自己公开 Cancel() 的类。方法。


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

相反,您可以实现您的 Run()方法为 async ,并拥有它 await如果和何时 TaskCompletionSource可用。然后它将有公共(public)方法 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;
}

关于c# - CancellationTokenSource 需要建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115965/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com