1

I use async/await extensively almost everywehrre. But I saw it enough in a few places that brought my attension.

What is the benefit of await task.ConfigureAwait(false) in C#?

Does turning on or off the flag affect the performance of a task? Why setting this continueOnCapturedContext to off recommended and why is it not false by default then?

This might be flagged as duplicate, but still couldn't come up with a solid and simple answer.

7
  • 3
    It's not really related to performance. It's do with avoiding deadlocks. Commented Aug 17, 2024 at 0:09
  • @Dai I thought using it for this purpose was a hack? Commented Aug 17, 2024 at 8:27
  • @GSerg Stephen's article isn't as clear as it could be (as it says "In your “library” async methods, use ConfigureAwait(false) wherever possible." - but then (seemingly) contradicts itself with "Using ConfigureAwait(false) to avoid deadlocks is a dangerous practice" - I think what Stephen is trying to say is that if you have code that calls Task.Result or Task.GetAwaiter().GetResult() (i.e. you have code that will block its thread if Task is incomplete) then you cannot rely on ConfigureAwait(false) as a workaround: you need to use await Task and ditch .Result etc. Commented Aug 17, 2024 at 8:38
  • 1
    @Dai No, what he is saying is that if third party code in your chain of calls omits the call to ConfigureAwait(false) even once, then you are out of luck, and your ConfigureAwait(false) is useless in preventing deadlocks. I believe the point he was trying to make is that the purpose of ConfigureAwait(false) is not to prevent deadlocks to begin with, which is further supported by blog.stephencleary.com/2023/11/configureawait-in-net-8.html ("That’s not its purpose, and it’s a questionable solution at best"). Commented Aug 17, 2024 at 10:15
  • 1
    thanks @TheodorZoulias will have a look at it, although is clear now quite a bit. Commented Aug 20, 2024 at 8:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.