如果指定一个方法是通过使用异步或异步修饰符异步方法,启用以下两个功能......被标记的异步方法本身可以通过调用它的方法等待。
但another page gives this example:
private async Task SumPageSizesAsync()
{
// To use the HttpClient type in desktop apps, you must include a using directive and add a
// reference for the System.Net.Http namespace.
HttpClient client = new HttpClient();
// . . .
Task<byte[]> getContentsTask = client.GetByteArrayAsync(url);
byte[] urlContents = await getContentsTask;
// Equivalently, now that you see how it works, you can write the same thing in a single line.
//byte[] urlContents = await client.GetByteArrayAsync(url);
// . . .
}
这里我们可以看到它调用一个函数GetByteArrayAsync
这是not equipped with the async keyword,然而主叫方能够等待的结果。
- 是否MSDN错误,当它说async修饰符使呼叫者等待?
- 从呼叫者的角度来看,返回
Task<T>
的函数与标记为异步的函数之间有什么区别?
如何协调您的答案与MSDN的文档,它是异步修改器,使呼叫者等待? –
因为如果你标记一个方法'async',你也必须改变它来返回一个'Task'而不是'T',否则代码将不能编译,返回一个Task的动作是允许调用者等待的,它实际上与async关键字无关,它只是新返回类型的副作用。 –