你的位置:首页 > ASP.net教程

[ASP.net教程]C# 异步编程小结


  1. APM 异步编程模型,Asynchronous Programming Model
  2. EAP 基于事件的异步编程模式,Event-based Asynchronous Pattern
  3. TAP 基于任务的异步编程模式,Task-based Asynchronous Pattern

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(基于事件的异步编程模式)



 EAP=>TAP(基于事件的异步编程模式转化为基于任务的异步编程模式)


 


 参考文章:http://www.dotblogs.com.tw/yc421206/archive/2013/08/06/113555.aspx