Using Func delegate with Async method

前端 未结 4 459
终归单人心
终归单人心 2020-12-24 10:05

I am trying to use Func with Async Method. And I am getting an error.

Cannot convert async lambda expression to delegate type \'Func

4条回答
  •  猫巷女王i
    2020-12-24 10:58

    Inside the Func run the task, wait for it and check for exception, then return the result.

    Func myFun = () => 
    {
       var t = Task.Run(async () => await myTask);
       t.Wait();
       if (t.IsFaulted)
          throw t.Exception;
       return t.Result;
    };
    

提交回复
热议问题