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?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; }
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?
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.Code:string x = Post(url, data).Result; return x;
confused.....
/S





Reply With Quote