我正在玩async-await和取消以获得更多关于此事的理解。为此,我做了以下控制台应用程序:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncTest
{
class Program
{
private static CancellationTokenSource _cancellationTokenSource;
private static CancellationToken _cancellationToken;
static void Main(string[] args)
{
Console.CancelKeyPress += myHandler;
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
var task = DoWorkAsync(_cancellationToken).ContinueWith(ContinueMethod);
task.Wait();
Console.ReadLine();
}
protected static void myHandler(object sender, ConsoleCancelEventArgs args)
{
if (_cancellationToken.CanBeCanceled)
{
_cancellationTokenSource.Cancel();
}
args.Cancel = true;
}
static void ContinueMethod(Task task)
{
if (task.IsCanceled)
{
Console.WriteLine("The task was canceled");
}
if (task.IsCompleted)
{
Console.WriteLine("The task completed successfully");
}
if (task.IsFaulted)
{
if (task.Exception != null)
{
var exceptions = task.Exception.Flatten().InnerExceptions;
foreach (var exception in exceptions)
{
Console.WriteLine(exception.Message);
}
}
Console.WriteLine("The task failed");
}
}
static async Task DoWorkAsync(CancellationToken cancellationToken)
{
await Task.Run(() => DoWork(cancellationToken), cancellationToken);
}
static void DoWork(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine("DoWork() is started");
// Uncomment the following line of code to put the task in a 'faulted' state
//throw new Exception();
for (var count = 0; count < 10; count++)
{
if (cancellationToken.IsCancellationRequested)
{
Console.WriteLine("Get a cancelation request");
cancellationToken.ThrowIfCancellationRequested();
}
else
{
Thread.Sleep(500);
Console.WriteLine("Count : " + count);
}
}
Console.WriteLine("DoWork() is finished");
}
}
}
当我让应用程序完成时,我正确地收到“任务已成功完成”消息。
现在,当我按下CTRL + C,触发已启动任务的取消(请参阅myHandler
拦截)时,我正确地收到“任务已取消”消息。但我也得到了“任务成功完成”的消息。我没想到任务也会显示为完整,因为我取消了它。
如果我取消注释throw new Exception();
方法中的DoWork()
行,我会正确收到“任务失败”消息,但也会收到“任务已成功完成”消息。
我的假设错了,这是设计的吗?或者我完全错过了其他什么?
我可以通过添加额外的检查来解决这个问题:
if (task.IsCompleted && !task.IsCanceled)
{
Console.WriteLine("The task completed successfully");
}
但我不确定这是否是正确的方法,或者我的程序中的其他内容是否导致此完成状态。
提前感谢您对此事的意见和/或澄清。
答案 0 :(得分:9)
Task.IsCompleted的文档说
当任务处于以下三种最终状态之一时,IsCompleted将返回true:RanToCompletion,Faulted或Cancelled。
所以IsCompleted
告诉你至少该任务不再运行。它不表示任务是否成功完成,失败或被取消。