当我执行 Task.Cancel 方法时抛出异常。我错过了什么吗?
Exception is thrown when I execute Task.Cancel method. Am I missing something?
当我单击 btnStart
时,循环开始并将值添加到 listBox1
。我想在单击 btnPause
时暂停执行,然后在单击 btnStart
时再次继续执行。我如何实现这一目标?请帮我。下面的代码我试过但没有运气。
CancellationTokenSource source = new CancellationTokenSource();
private void btnStart_Click(object sender, EventArgs e)
{
//here I want to start/continue the execution
StartLoop();
}
private async void StartLoop()
{
for(int i = 0; i < 999999; i++)
{
await Task.Delay(1000, source.Token);
listBox1.Items.Add("Current value of i = " + i);
listBox1.Refresh();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
//here I want to pause/stop the execution
source.Cancel();
}
您可以使用 PauseTokenSource
/PauseToken
combo from Stephen Cleary's Nito.AsyncEx.Coordination 包。它与 CancellationTokenSource
/CancellationToken
组合的概念类似,但它不是取消,而是暂停等待令牌的工作流。
PauseTokenSource _pauseSource;
CancellationTokenSource _cancelSource;
Task _loopTask;
private async void btnStart_Click(object sender, EventArgs e)
{
if (_loopTask == null)
{
_pauseSource = new PauseTokenSource() { IsPaused = false };
_cancelSource = new CancellationTokenSource();
_loopTask = StartLoop();
try { await _loopTask; } // Propagate possible exception
catch (OperationCanceledException) { } // Ignore cancellation error
finally { _loopTask = null; }
}
else
{
_pauseSource.IsPaused = false;
}
}
private async Task StartLoop()
{
for (int i = 0; i < 999999; i++)
{
await Task.Delay(1000, _cancelSource.Token);
await _pauseSource.Token.WaitWhilePausedAsync(_cancelSource.Token);
listBox1.Items.Add("Current value of i = " + i);
listBox1.Refresh();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
_pauseSource.IsPaused = true;
}
private async void btnStop_Click(object sender, EventArgs e)
{
_cancelSource.Cancel();
}
当我单击 btnStart
时,循环开始并将值添加到 listBox1
。我想在单击 btnPause
时暂停执行,然后在单击 btnStart
时再次继续执行。我如何实现这一目标?请帮我。下面的代码我试过但没有运气。
CancellationTokenSource source = new CancellationTokenSource();
private void btnStart_Click(object sender, EventArgs e)
{
//here I want to start/continue the execution
StartLoop();
}
private async void StartLoop()
{
for(int i = 0; i < 999999; i++)
{
await Task.Delay(1000, source.Token);
listBox1.Items.Add("Current value of i = " + i);
listBox1.Refresh();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
//here I want to pause/stop the execution
source.Cancel();
}
您可以使用 PauseTokenSource
/PauseToken
combo from Stephen Cleary's Nito.AsyncEx.Coordination 包。它与 CancellationTokenSource
/CancellationToken
组合的概念类似,但它不是取消,而是暂停等待令牌的工作流。
PauseTokenSource _pauseSource;
CancellationTokenSource _cancelSource;
Task _loopTask;
private async void btnStart_Click(object sender, EventArgs e)
{
if (_loopTask == null)
{
_pauseSource = new PauseTokenSource() { IsPaused = false };
_cancelSource = new CancellationTokenSource();
_loopTask = StartLoop();
try { await _loopTask; } // Propagate possible exception
catch (OperationCanceledException) { } // Ignore cancellation error
finally { _loopTask = null; }
}
else
{
_pauseSource.IsPaused = false;
}
}
private async Task StartLoop()
{
for (int i = 0; i < 999999; i++)
{
await Task.Delay(1000, _cancelSource.Token);
await _pauseSource.Token.WaitWhilePausedAsync(_cancelSource.Token);
listBox1.Items.Add("Current value of i = " + i);
listBox1.Refresh();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
_pauseSource.IsPaused = true;
}
private async void btnStop_Click(object sender, EventArgs e)
{
_cancelSource.Cancel();
}