What are the differences between using ConfigureAwait(false) and Task.Run?

后端 未结 4 1971
感动是毒
感动是毒 2020-12-22 22:53

I understand that it\'s recommended to use ConfigureAwait(false) for awaits in library code so that subsequent code does not run in the caller\'s e

4条回答
  •  春和景丽
    2020-12-22 23:20

    In this case, your Task.Run version will have a bit more overhead, as the first await call (await client.GetAsync(address)) will still marshal back into the calling context, as will the results of the Task.Run call.

    In the first example, on the other hand, your first Async() method is configured to not require marshaling back into the calling context, which allows the continuation to run on a background thread still. As such, there won't be any marshaling back into the caller's context.

提交回复
热议问题