Using Microsoft.bcl.async in PCL with Mono Droid?

I have a profile-oriented portable class library (PCL) 158 (Windows Store, .NET 4.5, Silverlight 5, Windows Phone 8). I can easily work with methods that return the type Task, and it all works as I expected. Whenever I access the Result property, it exits asynchronous code and returns the result.

However, if I use the async / await keywords in a method inside the PCL, I return the task. However, when I try to access the Result property, it blocks and never returns.

Looking at the debug output window in Visual Studio, in both cases I see the same text:

Thread started: 
Thread started: 
Loaded assembly: Mono.Security.dll [External]
Thread started: 
Thread started: 

So it looks like the code is starting, but it never returns to the user interface thread. Has anyone else tried using PCL with Microsoft.bcl.async in PCL?

My Mono Droid project is for Android 2.1.

Thanks, John

Update:
The following is additional information about the various scenarios. Firstly, here is the code that works on Mono Droid when it is written in user interface code:

var task = request.GetResponseAsync();
string html = task.Result.GetResponseText();

Then I created the following method in PCL:

public async Task<string> Test()
{
    IHttpResponse responce = await GetResponseAsync();
    return responce.GetResponseText();
}

And name it using this code from Mono UI code:

string html = request.Test().Result;

he never returns ...

+5
source share
1 answer

This is a classic deadlock scenario as I describe on my blog.

await "" async. "" SynchronizationContext, null, TaskScheduler.

, async ( SynchronizationContext), , Result. async , , .

, :

  • async . Result Wait Task, async; await .
  • ConfigureAwait(false), , .

async/await intro.

+3

All Articles