xamarin/Xamarin.Auth — pull request #23 by gaearon (2013-06-04 18:59:14+00:00)
Title: Avoid blocking thread by calling .Result on a non-completed task

This code blocks a thread pool thread until response is received:

return Task.Factory
.FromAsync (request.BeginGetResponse, request.EndGetResponse, null)
.ContinueWith (resTask => {
return new Response ((HttpWebResponse)resTask.Result);
}, cancellationToken).Result;

(note `.Result` call at the end)

This is not very nice, and in some cases we found that the continuation never gets executed. I investigated this and I think that (at least in some cases) `FromAsync` task scheduler doesn't allow more than N concurrent tasks, and this may have caused a deadlock between the continuation and the parent task waiting for its `Result`.

This pull request uses continuations and `TaskCompletionSource` instead of synchronous `Result` call to avoid blocking the thread until response is received.

When I removed `Result` call, I was no more experiencing the hangs.
Does this make sense?