C# 带有CancellationToken的任务状态不为';不变
我正在实现一个downloader类并使用Tasks,首先我创建了一个task并将CancellationToken传递给它,当我取消该task时,该task被取消,但该task的状态仍在运行,即使在调用ThrowifcCancellationRequested()之后,状态仍然相同。如果我抛出ThrowIfCancellationRequested(),状态不应该被取消或出错吗 这是我的密码:C# 带有CancellationToken的任务状态不为';不变,c#,winforms,task,cancellation,cancellationtokensource,C#,Winforms,Task,Cancellation,Cancellationtokensource,我正在实现一个downloader类并使用Tasks,首先我创建了一个task并将CancellationToken传递给它,当我取消该task时,该task被取消,但该task的状态仍在运行,即使在调用ThrowifcCancellationRequested()之后,状态仍然相同。如果我抛出ThrowIfCancellationRequested(),状态不应该被取消或出错吗 这是我的密码: public void Start() { //Start down
public void Start()
{
//Start download
Status = DownloadStatus.Running;
CancellTS = new CancellationTokenSource();
CancelT = CancellTS.Token;
TaskStartDownload = Task.Run(() => { StartDownload(); }, CancelT);
}
private void StartDownload()
{
//Raise event download start
if (OnDownloadStart != null)
OnDownloadStart(this, new DownloadEventArgs(Id, 0, 0, 0, Controls, false));
byte[] buffer = new byte[4096];
long bytesToRead = Size;
Timer.Start();
while (!CancelT.IsCancellationRequested)
{
int currentBytesRead = Reader.Read(buffer, 0, buffer.Length);
Speed = currentBytesRead == 0 ? 0 : ((float)BytesRead / 1024) / Timer.Elapsed.TotalSeconds;
ProgressPercentage = ProgressPercentage + (currentBytesRead * 1.0 / Size) * 100;
TimeRemaining = (int)(((double)(Size - BytesRead) / 1024) / Speed);
if (currentBytesRead == 0)
{
Status = DownloadStatus.Completed;
//Raise event download start
if (OnDownloadComplete != null)
OnDownloadComplete(this, new DownloadEventArgs(Id, 100, 0, 0, Controls, false));
break;
}
Writer.Write(buffer, 0, currentBytesRead);
Writer.Flush();
//Raise progress event
this.OnDownloadProgress(this, new DownloadEventArgs(Id, Speed, ProgressPercentage, TimeRemaining, Controls, false));
BytesRead += currentBytesRead;
if (Status == DownloadStatus.Pause || Status == DownloadStatus.Cancel)
break;
}
Timer.Stop();
Reader.Close();
Reader = null;
Writer.Close();
if (CancelT.IsCancellationRequested)
{
try
{
CancelT.ThrowIfCancellationRequested();
}
catch (Exception ex)
{
Status = DownloadStatus.Cancel;
//Raise event download start
if (OnDownloadCancel != null)
this.OnDownloadCancel(this, new DownloadEventArgs(Id, 0, 0, 0, Controls, true));
File.Delete(FileNameWithPath);
}
}
TaskPrepareDownload.Dispose();
TaskStartDownload.Dispose();
}
public void Cancel()
{
if (TaskStartDownload == null)
{
return;
}
else
{
CancellTS.Cancel();
}
}
调用ThrowIfCancellationRequested()后,不应吞咽异常,以便可以将任务状态的更改通知调用方(Start()方法)。如果您接受该异常,则认为该任务已成功完成(状态=RanToCompletion),或者如果您在触发取消后立即轮询该任务,则该任务仍在运行 当您等待任务完成时,应将异常处理程序放置在调用方中 请参考MSDN中的示例