In order not to block the UI, the synchronous model is completely removed from the multithreading of Windows Phone, and all requests are processed using asynchronous models.
The advantage of this is that the UI will not be stuck, it can always respond to the user's response, it will waste time-consuming operations to the background thread for processing, and it can avoid the trouble of thread synchronization.
However, the written code is also ugly and may involve data sharing.
I believe that if your application involves httprequest, you will surely encounter what I said.
You are welcome to discuss your ideas.
Below are several models of commonly used asynchronous request code
1. Use anonymous functions. Sometimes Lamda expressions are used to save money.
2. Event Mode: Subscribe to requests to complete events
3. async CTP
The code for the first method is as follows:
Private string reqest () {string resultstring = string. empty; httpwebrequest request = httpwebrequest. createhttp ("http://www.google.com"); Request. method = "get"; request. begingetresponse (iasyncresult result) => {httpwebrequest webrequest = result. asyncstate as httpwebrequest; httpwebresponse webresponse = (httpwebresponse) webrequest. endgetresponse (result); stream streamresult = webresponse. getresponsestream (); streamreader reader = new streamreader (streamresult); // The returned value resultstring = reader. readtoend () ;}, request); Return resultstring ;}
-
The code for the second method is as follows:
Public Delegate void getresulteventhandler (Object sender, string E); public event getresulteventhandler ongetinfocompleted; private void reqest () {httpwebrequest request = httpwebrequest. createhttp ("http://www.google.com"); Request. method = "get"; request. begingetresponse (getinfocompleted, request);} protected void getinfocompleted (iasyncresult asyncresult) {try {httpwebrequest webrequest = asy Ncresult. asyncstate as httpwebrequest; httpwebresponse webresponse = (httpwebresponse) webrequest. endgetresponse (asyncresult); stream streamresult = webresponse. getresponsestream (); streamreader reader = new streamreader (streamresult); // The returned value string resultstring = reader. readtoend (); If (ongetinfocompleted! = NULL) {ongetinfocompleted (this, resultstring) ;}} catch (webexception ex) {MessageBox. Show (ex. Message );}}
The third method of code implementation is as follows:
private async resultString Reqest() { HttpWebRequest request = HttpWebRequest.CreateHttp("http://www.google.com"); request.Method = "GET"; HttpWebResponse webResponse = await request.GetResponseAsync(); Stream streamResult = response.GetResponseStream(); StreamReader reader = new StreamReader(streamResult, new GB2312.GB2312Encoding()); string resultString = reader.ReadToEnd(); return resultString; }
-
Sometimes the request has a nested relationship. For example, before requesting the second data, you need to request the first data first. Therefore, the first method will remain anonymous at many layers, which makes me feel uncomfortable.
I have been using the second method to write code without async CTP for Windows Phone, because the logic is clear. The event model does not seem perfect, but it is also a common asynchronous model that uses events to transmit parameters. The disadvantage is that it cannot be blocked when the logic needs to block the thread.
I think this is also the reason why the async framework appears.
In the development of Windows 8, The async framework has become the official version. That is to say, it fully supports the third asynchronous model, that is, the model dominated by async and await. In this way, the code to be waited can be blocked, the UI thread can be kept unobstructed, and the code logic can be very clear, and the thread synchronization data lock is avoided.
This is a perfect solution at this stage.
Let's discuss it actively.