Windows Phone does not exist multi thread? On Http post question

The execution of PostResult function to _PostDone.Wait (); prevent the GetResponse function (when another thread) executive, is not the windows phone does not support multiple threads.


ResultData PostResult(MemoryStream stream)
{
//string url = SERVICE_URL + "test/";
string hello = Trace(stream);
stream.Position = 0;
HttpWebRequest request = HttpWebRequest.CreateHttp(SERVICE_URL + "authterm/?termid=1000&digest=1234");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = stream.Length;
request.Method = "POST";
request.AllowReadStreamBuffering = true;

try
{
request.BeginGetRequestStream((asyncResult) =>
{
byte[] header = System.Text.Encoding.UTF8.GetBytes(hello);
HttpWebRequest postRequest = asyncResult.AsyncState as HttpWebRequest;
Stream streamResult = postRequest.EndGetRequestStream(asyncResult);

//Get the response stream
streamResult.Write(stream.ToArray(), 0, (int)stream.Length);
//streamResult.Write(header, 0, header.Length);
streamResult.Flush();
streamResult.Close();

Thread getThread = new Thread(new ParameterizedThreadStart(GetResponse));
getThread.Start(postRequest);

}, request);
_PostDoned.WaitOne();

return new ResultData();
}
catch (WebException ex)
{
return null;
}
}

private void GetResponse(object param)
{
HttpWebRequest postRequest = param as HttpWebRequest;
postRequest.BeginGetResponse((asyncPostResult) =>
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asyncPostResult.AsyncState;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncPostResult);

Stream responseStream = webResponse.GetResponseStream();
DataContractJsonSerializer rspDataSerializer = new DataContractJsonSerializer(typeof(ResultData));
Result = (ResultData)rspDataSerializer.ReadObject(responseStream);
_PostDoned.Set();
}
catch (Exception ex)
{
throw new Exception();
}
}, postRequest);
}

Started by Ken at December 08, 2013 - 8:45 AM

APM mode interface you abruptly written like this. . . . . .

Posted by Albert at December 12, 2013 - 9:01 AM

Give you a reference.

public event EventHandler<String> PostCompleted;
private void Post(String postUrl, byte[] postBody)
{
WebRequest webRequest = HttpWebRequest.Create(postUrl);
webRequest.Method = "POST";
webRequest.ContentLength = postBody.Length;
//Set the request header
//webRequest.Headers["xxkey"] = "xxxvalue";
webRequest.BeginGetRequestStream((ar) =>
{

WebRequest request = (WebRequest)ar.AsyncState;
using (Stream postBodyStream = request.EndGetRequestStream(ar))
{
postBodyStream.Write(postBody, 0, postBody.Length);
}
request.BeginGetResponse(EndResponseCallback, request);
}, webRequest);
}

private void EndResponseCallback(IAsyncResult ar)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
if (request.HaveResponse == false)
{
//The error report
return;
}
WebResponse response = request.EndGetResponse(ar);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
String result = reader.ReadToEnd();
OnPostCompleted(result);
}
}

protected void OnPostCompleted(String e)
{
var postCompletedEventHandler = this.PostCompleted;
if (postCompletedEventHandler == null)
{
return;
}
postCompletedEventHandler(this, e);
}

Posted by Albert at December 16, 2013 - 9:07 AM

Oh, you told me to write is the same, but not synchronous, with a ManualResetEvent (_PostDoned.Wait); will make the other thread to block.

Posted by Ken at December 22, 2013 - 10:07 AM

No problem ah

Posted by Ken at December 29, 2013 - 11:06 AM

windows phone, Why are you synchronization?
Change a way of thinking, not to the main card UI.?

Posted by Albert at January 12, 2014 - 11:42 AM

I have put the code into asynchronous, and the program running no problem, I just wondered, windows phone seems to have no real sense of multi thread?

Posted by Ken at January 20, 2014 - 12:01 PM

The arrangement of WIN8 can do.

Posted by Reginald at February 02, 2014 - 12:20 PM

Our aim is asynchronous, a way of multi thread is asynchronous.

Can say, the internal APM model of this begin\end is multi thread, a thread pool by system maintenance to do.

In WP, you can use the thread, but the internal blocking method is not implemented (remember to turn the source when seen, not sure), you want to know more just to decompile the source code to understand

Posted by Albert at February 13, 2014 - 12:41 PM

One of the characteristics of Microsoft SDK is, he thinks he can do, do not need you to do, you are not allowed to do.

Posted by Albert at February 18, 2014 - 1:01 PM

Ha ha, powerful, long knowledge.

Posted by Ken at February 24, 2014 - 1:35 PM