You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已通过await等待的Task其IsCompleted属性是否可能返回false?

Can task.IsCompleted be false after awaiting the task?

Short answer: Absolutely not — once you've awaited a Task, it is guaranteed to be in a completed state, so task.IsCompleted will always return true at that point.

Let me break this down for you:

How await works

When you await a Task, the runtime will pause execution of the current method until the task reaches one of its final states: RanToCompletion, Faulted, or Canceled. It doesn't matter if the task completes synchronously (right when you call DoSomethingAsync) or asynchronously later — the await keyword ensures your code doesn't proceed past that line until the task is done.

What this means for your code

Your original code:

Task task = DoSomethingAsync( someObject );
await task.ConfigureAwait( false );
if ( task.IsCompleted ){ ... }

That if check is completely redundant. After await task.ConfigureAwait(false);, task is guaranteed to be completed, so the condition will always evaluate to true.

Safe code replacement

You can absolutely replace this with:

await DoSomethingAsync( someObject ).ConfigureAwait( false );

And remove the if statement entirely. This code behaves exactly the same as your original, but is cleaner and avoids unnecessary checks.

Confirming with the IsCompleted documentation

As you noted, the docs state:

如果任务已完成(即任务处于RanToCompletion、Faulted或Canceled这三种最终状态之一),则返回true;否则返回false。

Since await only proceeds when the task is in one of these three final states, IsCompleted can't be false after awaiting. There's no scenario where this could happen — it's a fundamental guarantee of how async/await works in .NET.

内容的提问来源于stack exchange,提问作者Mong Zhu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 00:58:10