你的位置:首页 > 软件开发 > ASP.net > C# 异步编程小结

C# 异步编程小结

发布时间:2015-09-26 18:00:41
APM 异步编程模型,Asynchronous Programming ModelEAP 基于事件的异步编程模式,Event-based Asynchronous PatternTAP 基于任务的异步编程模式,Task-based Asynchronous PatternAPM( ...
  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)        {          get='_blank'>string stringContent;          stringContent = myRequestState.requestData.ToString();          Console.WriteLine(stringContent);        }        Console.WriteLine("Press any key to continue..........");        Console.ReadLine();        responseStream.Close();      }    }  }}

原标题:C# 异步编程小结

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录