News for hackers. Stuff that matters.
Re:Asynchronous Programming in C# by piedar on Friday September 24, 2021 @01:13PM (#28645216)
> var myValue = DoSomethingAsync().ConfigureAwait(false).Result;

Came out a decade ago and we still don't know how to use it safely.

This example doesn't compile because there is no Result on ConfiguredTaskAwaitable. Regardless, ConfigureAwait(false) does absolutely nothing here because this Task is not being awaited.

If you're going to block this thread, you must push the work to another thread or it's going to deadlock when the implementation tries to resume a continuation (unless the implementation is 100% perfect and the SynchronizationContext smiles upon you).

var result = Task.Run(() => CalculateAsync()).GetAwaiter().GetResult();

This avoids the deadlock but can lead to other nasty things like thread pool starvation. The only true solution is to go async all the way - https://blog.stephencleary.com/2012/07/dont-block-on-async-c...

Re:Asynchronous Programming in C# by sebazzz on Saturday September 25, 2021 @02:39AM (#28651014)
> Regardless, ConfigureAwait(false) does absolutely nothing here because this Task is not being awaited.

It does help if there is a SynchronisationContext active, like in legacy ASP.NET

Re:Asynchronous Programming in C# by rawling on Saturday September 25, 2021 @07:19AM (#28652202)
No, really. ConfigureAwait configures the await. If you don't await - if you block by calling .Result - it does nothing