Task deadlocks because of Result

前端 未结 1 394
迷失自我
迷失自我 2020-12-21 20:58

I have the following construct: A base-class which does some loading tasks asynchronously and a inherited class which only converts the result of the base-class into a speci

相关标签:
1条回答
  • 2020-12-21 21:24

    Must I return a Task in the Process-method?

    That's the best solution, yes:

    public Task<TOut> ProcessAsync(object parameters)
    {
        return StartProcessorAsync(parameters);
    }
    

    What can I do so that this runs asynchronously whilst it still returns the object at the end?

    Those two statements don't make sense together. Think about it. You want it to run asynchronously and yet synchronously return the result.

    The best solution is to allow the code to be asynchronous. If StartProcessorAsync is asynchronous, then everything that calls it should be asynchronous, too. This is the natural way to write asynchronous code.

    There are various hacks to try to get synchronous over asynchronous working, but none of them work in all scenarios - because, in some way, every one of those hacks must try to force the asynchronous work to complete synchronously. That just doesn't work cleanly; the best solution is to allow asynchronous work to be asynchronous.

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