我正在使用 Microsoft.Bcl.Async nuget 包来使用 Async/Await(我的目标是 .NET 4,而不是 4.5)
我是异步/等待的新手。下面的代码示例中描述了我的问题。为什么没有调用突出显示的行?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncTest
{
class Program
{
static void Main(string[] args)
{
Run();
Console.WriteLine("Got the value");
Console.ReadKey();
}
public static async void Run()
{
Console.WriteLine("Testing Async...");
var val = await AsyncMethod();
// HIGHLIGHTED LINE. WHY IS THIS LINE NOT CALLED?
Console.WriteLine("And the value is... {0}", val);
}
public static Task<long> AsyncMethod()
{
long myVal = 1;
for (long l = 0; l < 100000; l++)
{
myVal++;
Console.WriteLine("l = {0}", l);
}
return new Task<long>(() => myVal);
}
}
}