private async static void Test2() { Func<object, int> func = obj => { return int.Parse(obj.ToString()) + 1; }; Task<int> t7 = Task.Run<int>(() => { return new Task<int>(func, 1, TaskCreationOptions.None); }, CancellationToken.None); Console.WriteLine("t7.Result:{0}", await t7); }
这段代码拿不到执行结果,找不到原因.
Test2的签名是有问题的。
应该改成这样:private async static TaskTest2()
然后这样调用:await Test2();
static void Main(string[] args) { Task.WaitAll(Test2()); Console.Read(); } private static async Task Test2() { Func<object, int> func = (obj) => int.Parse(obj.ToString()) + 1; var t7 = Task.Run<int>(() => func("11")); Console.WriteLine("t7.Result:{0}", (await t7)); }