Why use async when I have to use await?

后端 未结 3 1063
挽巷
挽巷 2020-12-25 12:18

I\'ve been stuck on this question for a while and haven\'t really found any useful clarification as to why this is.

If I have an async method like:

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-25 12:46

    If I use await here, what's the point of the asynchronous method?

    await does not block thread. MyMethod2 will run synchronously until it reaches await expression. Then MyMethod2 will be suspended until awaited task (MyMethod) is complete. While MyMethod is not completed control will return to caller of MyMethod2. That's the point of await - caller will continue doing it's job.

    Doesn't it make the async useless that VS is telling me to call await?

    async is just a flag which means 'somewhere in the method you have one or more await'.

    Does that not defeat the purpose of offloading a task to a thread without waiting for it to finish?

    As described above, you don't have to wait for task to finish. Nothing is blocked here.

    NOTE: To follow framework naming standards I suggest you to add Async suffix to asynchronous method names.

提交回复
热议问题