I have the following extension method:
internal static string ReadLine(this DataReader stream)
{
Task<string> line = ReadLineAsync(stream);
// line.Wait(); - Not Required, as next call blocks
return line.Result;
}
It’s basically a Synchronous method call wrapper for an Asynchronous method call that returns a string. The code works fine if I step through it line by line, however it seems that if I let it execute freely, I encounter an indefinite block. Any ideas?
Related to a previous question I posted: How can I update my API to use the async keyword without using await for all callers
As some commented on the answer to your other question, if you use
Task.Resultin a GUI application, you may cause a deadlock (as I explain in detail on my blog).In short:
linerepresents theReadLineAsyncmethod, and will complete when that method completes.ReadLineAsynccallsawaiton some operation that is incomplete. This causesReadLineAsyncto return an incomplete task (line).lineto complete.awaited operation completes, it schedules the remainder ofReadLineAsyncto the UI thread.ReadLineAsyncbecause it is synchronously blocked waiting forReadLineAsyncto complete. Deadlock.See my answer to your other question for a way around this deadlock. In short:
ConfigureAwait(false)everywhere.Resultwrapping its errors inAggregateException.