Why is AsyncContext needed when using async/await with a console application?

后端 未结 1 1881
日久生厌
日久生厌 2021-02-13 04:28

I\'m calling an async method within my console application. I don\'t want the app to quit shortly after it starts, i.e. before the awaitable tasks complete. It seems like I ca

相关标签:
1条回答
  • 2021-02-13 05:20

    It's not required; it's just my preference.

    You can synchronously block on a task within Main (using Wait/Result/WaitAll). The semantics are slightly different; in particular, if the async code fails, then Wait/Result/WaitAll will wrap the exception in an AggregateException, while AsyncContext does not.

    Also, AsyncContext treats the main thread specially; instead of sending continuations to the thread pool, it will send them back to that main thread (by default; you can always use ConfigureAwait(false) to avoid this). I find this useful if I'm writing a "proof of concept" console app, because AsyncContext behaves very similarly to the UI contexts.

    But at the end of the day, it's just a matter of preference.

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