1

我正在实现一个下载器类并且我正在使用任务,首先我创建了一个任务并通过它传递了 CancellationToken,当我取消任务时,任务被取消但任务的状态仍然是运行,即使在我调用 ThrowIfCancellationRequested( ) 状态还是一样的。如果我抛出 ThrowIfCancellationRequested(),状态是否假定为 Canceled 或 Faulted。

这是我的代码:

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

1 回答 1

3

调用 ThrowIfCancellationRequested() 后不应吞下异常,因此可以通知调用者(Start() 方法)任务状态的变化。如果您吞下异常,则认为该任务已成功完成(状态 = RanToCompletion),或者如果您在触发取消后立即轮询它,则该任务仍处于运行状态。

当您等待任务完成时,应将异常处理程序放在调用者中。

请参考 MSDN Task Cancellation中的示例。

于 2014-08-26T02:45:47.317 回答