APM(早期的异步编程模型)
该编程模型使用BeginXXX,EndXXX方法和IAsyncResult接口
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace AsyncProgramTest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public class RequestState { // This class stores the State of the request. const int BUFFER_SIZE = 1024; public StringBuilder requestData; public byte[] BufferRead; public HttpWebRequest request; public HttpWebResponse response; public Stream streamResponse; public RequestState() { BufferRead = new byte[BUFFER_SIZE]; requestData = new StringBuilder(""); request = null; streamResponse = null; } } public MainWindow() { InitializeComponent(); } const int BUFFER_SIZE = 1024; private void button_Click(object sender, RoutedEventArgs e) { HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/"); RequestState myRequestState = new RequestState(); myRequestState.request = myHttpWebRequest; IAsyncResult result =(IAsyncResult)myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState); } private void RespCallback(IAsyncResult asynchronousResult) { RequestState myRequestState = (RequestState)asynchronousResult.AsyncState; HttpWebRequest myHttpWebRequest = myRequestState.request; myRequestState.response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(asynchronousResult); // Read the response into a Stream object. Stream responseStream = myRequestState.response.GetResponseStream(); myRequestState.streamResponse = responseStream; // Begin the Reading of the contents of the HTML page and print it to the console. IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState); return; } private static void ReadCallBack(IAsyncResult asyncResult) { RequestState myRequestState = (RequestState)asyncResult.AsyncState; Stream responseStream = myRequestState.streamResponse; int read = responseStream.EndRead(asyncResult); // Read the HTML page and then print it to the console. if (read > 0) { myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read)); IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState); return; } else { Console.WriteLine("\nThe contents of the Html page are : "); if (myRequestState.requestData.Length > 1) { string stringContent; stringContent = myRequestState.requestData.ToString(); Console.WriteLine(stringContent); } Console.WriteLine("Press any key to continue.........."); Console.ReadLine(); responseStream.Close(); } } } }
EAP(基于事件的异步编程模式)
TAP(基于任务的异步编程模式)
private async void button_Click(object sender, RoutedEventArgs e) { System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(); var msg = await httpClient.GetAsync("http://www.cnblogs.com/"); var htmlString=await msg.Content.ReadAsStringAsync(); //todo process htmlstring }
EAP=>TAP(基于事件的异步编程模式转化为基于任务的异步编程模式)
APM=>TAP(BeginXXX转化为基于任务的异步编程模型)
private async void button_Click(object sender, RoutedEventArgs e) { HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/"); var maskTask = Task.Factory.FromAsync<WebResponse>(myHttpWebRequest.BeginGetResponse,myHttpWebRequest.EndGetResponse, null, TaskCreationOptions.None); WebResponse webResponse = await maskTask; Stream responseStream = webResponse.GetResponseStream(); using (StreamReader reader = new StreamReader(responseStream)) { var htmlString =await reader.ReadToEndAsync(); Console.WriteLine(htmlString); //to do process html String } }
参考文章:http://www.dotblogs.com.tw/yc421206/archive/2013/08/06/113555.aspx
原文:http://www.cnblogs.com/LittleFeiHu/p/4841002.html