In many cases, the request is usually made synchronously, until the response is followed by logical processing, but sometimes the subsequent logical processing does not depend on the result of the request or can be suspended until the response is processed, or in order to resolve the UI "suspended animation" phenomenon, you can use the asynchronous request
Asynchronously gets the result of the response using the BeginGetResponse method in the WebRequest instance, where the parameter callback is a delegate type, the state is a custom object, and state is used to hold some information. You may need to use this information in the callback corresponding method.
The following example illustrates the assumption that a request URL, which corresponds to a picture, is obtained asynchronously and then presented to PictureBox (the example is based on a form in WinForm)
The variables that hold the data are defined in the WinForm
list<bytenew list<byte>(); byte New byte [8];
Create a WebRequest instance where you specifically need to request the server, and call the BeginGetResponse method
string " http://localhost/1.jpg " ; var request = webrequest.create (URL); request. BeginGetResponse (respcallback, request);
The Respcallback method is as follows, here ar. AsyncState is the information that is called the parameter state in the BeginGetResponse method above, which is the request
Private void respcallback (IAsyncResult ar) { var as WebRequest; var response = Request? . EndGetResponse (AR); = response? . GetResponseStream (); Responsestream08, Readcallback, Responsestream);}
Because it is asynchronous, the read data here is also read asynchronously, Readcallback as follows:
Private voidreadcallback (IAsyncResult ar) {using(varResponsestream = ar. AsyncState asStream) { if(Responsestream = =NULL)return; varRead =responsestream.endread (AR); if(Read >0) {lstbuffer.addrange (buffers); Responsestream.beginread (buffers,0,8, Readcallback, Responsestream); } Else{lstbuffer.addrange (buffers); Stream s=NewMemoryStream (Lstbuffer.toarray ()); pictureBox1.Image=Image.fromstream (s); Picturebox1.sizemode=Pictureboxsizemode.zoom; Responsestream.dispose (); } }}
Each time a certain byte is read, it is populated into the specified container (lstbuffer) and is called responsestream.beginread (buffers, 0, if the data has not been read). 8 , Readcallback, responsestream);
when the read is complete, the data that will be read (Lstbuffer) convert to a picture and assign a value to PictureBox.
If you are using WebClient in a much smaller amount of code, call the DownloadDataAsync method to initiate an asynchronous request to register the event downloaddatacompleted, triggers when an asynchronous request is completed wc_downloaddatacompleted Events, in the wc_downloaddatacompleted event of the DownloadDataCompletedEventArgs type Parameters e contains some information, where E. Result is the outcome of the request response (byte[] type)
var New WebClient (); WC. DownloadDataAsync (new Uri ("http://localhost/1.jpg"+ = wc_ downloaddatacompleted;
Private void Wc_downloaddatacompleted (object sender, DownloadDataCompletedEventArgs e) { using New MemoryStream (e.result)) { ifnull) pictureBox1.Image = Image.fromstream (ms);} }
. Net WebRequest asynchronous requests and WebClient asynchronous requests