I've spent many hours trying to figure this one out, but unfortunately to no avail. I'd be grateful for any help.
The problem is that the PostAsync
call blocks event though I use await
and ConfigureAwait(false)
. The below handler is called on the UI thread:
private async void OnApplicationLaunching(object sender, LaunchingEventArgs e)
{
using (var httpClient = new HttpClient())
{
var content = new List<KeyValuePair<string, string>>();
var urlEncodedContent = new FormUrlEncodedContent(content);
await httpClient.PostAsync("url address", urlEncodedContent).ConfigureAwait(false);
}
}
Edit 1
- I use WindowsPhone 8.0 silverlight
- In my opinion it is a deadlock. The execution is blocked until the exception appears with "task got canceled" message
- I am not interested in "fire and forget". I need to be sure that the data is sent before allowing further execution.
Edit 2
Here's another code sample which does not differ alot from previous one, but beter resembles the code samples from Stephen's blog
Section "Preventing the Deadlock" in http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
Section "Avoiding Context" in http://blog.stephencleary.com/2012/02/async-and-await.html
private async void OnApplicationLaunching(object sender, LaunchingEventArgs e)
{
await this.SendSth();
// here I want to know for sure that data has already been sent
}
private async Task SendSth()
{
using (var httpClient = new HttpClient())
{
var content = new List<KeyValuePair<string, string>>();
var urlEncodedContent = new FormUrlEncodedContent(content);
await httpClient.PostAsync("some url", urlEncodedContent).ConfigureAwait(false);
}
}
By the way, thanks Stephen for your great articles. They help alot.