News for hackers. Stuff that matters.
Re:Asynchronous Programming in C# by spaetzleesser on Friday September 24, 2021 @10:45AM (#28643128)
"var myValue = DoSomethingAsync().ConfigureAwait(false).Result;"

In my view there should be a built-in keyword to do this right. It's too easy to get this wrong and even worse possible problems only show up rarely.

Re:Asynchronous Programming in C# by jtbarrett on Friday September 24, 2021 @11:43AM (#28643946)
If this really needs to happen then I like .GetAwaiter().GetResult() instead of .Result to get the same exception behavior as await rather than the wrapped AggregateException that .Result throws. This is especially helpful if DoSomethingAsync sometimes throws synchronously rather than returning a task.
Re:Asynchronous Programming in C# by gregoryl on Friday September 24, 2021 @11:14AM (#28643555)
The reason there isn't a keyword is because its not possible to do it right. The example given is far from foolproof.
Re:Asynchronous Programming in C# by spaetzleesser on Friday September 24, 2021 @03:41PM (#28646911)
But there should be an easy way to do it right. Needing to call async functions from non-async code is not exactly an unusual thing. Or they need to make absolutely everything async which is also problematic, especially in terms of raw performance.
Re:Asynchronous Programming in C# by torginus on Friday September 24, 2021 @11:10AM (#28643508)
Well, you can put it into an extension method like this:

T SyncResult<T>(this Task<T> task){ return task.ConfigureAwait(false).Result; }

Same for ValueTask. It may already be implemented in the base .NET library as well.