|
-
Feb 19th, 2013, 09:10 AM
#1
Thread Starter
Frenzied Member
Why does this code stop?
Code:
private async Task<string> Post(string url, string postdata)
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "message/rfc822";
byte[] data = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = data.Length;
using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
return received;
}
The above code halts at the using statement. The method is called from a windows phone 8 Project, where I know they run async methods on the UI thread. But surely isn't async/wait the way to handle this?
What I want is this request-response to fnish, then continue to execute the UI thread. Not to block it completly like this code does.
Is the code wrong? Do I call it wrong?
Code:
string x = Post(url, data).Result;
return x;
To be honest Im not 100% comfortable using the new syntax, and the "feature" windows phone 8 has to run async threads on UI thread, I dont like at all. It is somewhat strange, that if I ´do this as I use to with callbacks and stuff, the UI thread will continue execute while the code in the async method executes (as it should) but when I use an AutoResetEvent.WaitOne() I block Everything, even the async method.
confused.....
/S
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|