【发布时间】:2017-05-23 08:21:57
【问题描述】:
我正在关注@StephenHaunts 的这篇博文https://stephenhaunts.com/2014/10/10/simple-async-await-example-for-asynchronous-programming/#comments
LongRunningOperation 内部代码中最后一个返回语句的目的是什么。
private static async Task<string> LongRunningOperation()
{
int counter;
for (counter = 0; counter < 50000; counter++)
{
Console.WriteLine(counter);
}
return "Counter = " + counter;
}
现在我知道的是:
当我在
Task.Run中调用这个LongrunningOperation时,它应该返回这个等待的方法默认返回的任何内容。那么为什么它不这样做。如果我使用
Task.Result属性,那么它将同步运行并阻塞调用线程,不推荐这样做。
我想问的是:
如何在调用点打印这个返回值?
为什么@Stephen 在不需要的时候写了那个声明?
提前致谢。
【问题讨论】:
-
你说的在不需要的时候写了那个语句是什么意思?该方法的返回类型是
async Task<string>,当然你必须返回一个字符串,否则它不会编译。
标签: c# .net multithreading console async-await