Passing a task as parameter

前端 未结 1 1552
醉梦人生
醉梦人生 2020-12-17 10:15

I am not sure whether this is possible, so here me out:

I have a sequence of action to perform multiple

async Task MethodA(...)
{
    // some code
          


        
相关标签:
1条回答
  • Of course, simply invoke the func, get back a task, and await it:

    async Task Method(Func<Task<Entity>> func)
    {
        // some code
        var a = await func();
        // some code
    }
    

    Also, when you're sending that lambda expression, since all it's doing is calling an async method which in itself returns a task, it doesn't need to be async in itself:

    Method(() => CallToIOBoundTask(params));
    

    That's fine as long as all these calls return Task<Entity>. If not, you can only use Task (which means starting the operation and awaiting its completion) and you won't be able to use the result.

    0 讨论(0)
提交回复
热议问题