考虑这个例子:
public async Task<UserProfile> GetProfileAsync(Guid userId)
{
// First check the cache
UserProfile cached;
if (profileCache.TryGetValue(userId, out cached))
{
return cached;
}
// Nope, we'll have to ask a web service to load it...
UserProfile profile = await webService.FetchProfileAsync(userId);
profileCache[userId] = profile;
return profile;
}
现在想象一下调用一个在另一个异步方法:
public async Task<...> DoSomething(Guid userId)
{
// First get the profile...
UserProfile profile = await GetProfileAsync(userId);
// Now do something more useful with it...
}
这是完全有可能通过GetProfileAsync
返回的任务将已经通过的时间完成方法返回 - 因为缓存。或者,当然,您可能正在等待异步方法的结果以外的其他内容。
所以不,你声称等待时间不会在你等待的时候完成是不真实的。
还有其他原因。考虑以下代码:
public async Task<...> DoTwoThings()
{
// Start both tasks...
var firstTask = DoSomethingAsync();
var secondTask = DoSomethingElseAsync();
var firstResult = await firstTask;
var secondResult = await secondTask;
// Do something with firstResult and secondResult
}
这有可能是第二个任务将在第一个之前完成 - 在这种情况下,你的时间等待第二个任务,它将完成,您只需继续前进。
你一直说这个等待的“还没有开始”,但这只是你问题中的断言/假设,而不是你在任何文档中可以指出的东西。 –
@Damien_The_Unbeliever youre评论是没有帮助你知道很多....我只是读什么新阅读,它不理解。现在 - 读完这两个答案之后,事情就更加清楚了。 –