Get the current Task instance in an async method body

后端 未结 1 688
渐次进展
渐次进展 2021-01-18 00:18

If I have an async method body like so -

public async Task GetSomething() {

    await SendText(\"hi\");
    await SendImage(\"bla.bmp\");

}
相关标签:
1条回答
  • 2021-01-18 00:58

    This is not directly possible as the language does not have a facility to access the "current" task.

    There is a workaround though: Wrap your async method in another method. This other method can get hold of the task once the async method returns (which happens approximately at the first await point).

    In all cases I recommend letting the caller add the async Task to your list, not the async method itself. This is useful even from an encapsulation point of view.

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