快速业务通道

浅谈.NET下的多线程和并行计算(十一).NET异步编程模型基础 下

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-20
uot;);
Console.WriteLine(DateTime.Now.ToString("mm:ss"));
Console.ReadLine();

进度更新事件处理方法:

static void wc_DownloadProgressChanged(object sender,  DownloadProgressChangedEventArgs e)
{
   Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",  (string)e.UserState, e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
}

完成下载事件处理方法:

static void wc_DownloadStringCompleted(object sender,  DownloadStringCompletedEventArgs e)
{
   Console.WriteLine(DateTime.Now.ToString("mm:ss"));
   Console.WriteLine(e.Result.Substring(0, 300));
}

程序输出结果:

浅谈.NET下的多线程和并行计算(十一).NET异步编程模型基础 下

我们可以看到WebClient的DownloadStringAsync方法在内部使用了WebRequest:

public void DownloadStringAsync(Uri address, object userToken)
{
   if (Logging.On)
   {
     Logging.Enter(Logging.Web, this, "DownloadStringAsync", address);
   }
   if (address == null)
   {
     throw new ArgumentNullException("address");
   }
   this.InitWebClientAsync();
   this.ClearWebClientState();
   AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(userToken);
   this.m_AsyncOp = asyncOp;
   try
   {
     WebRequest request = this.m_WebRequest = this.GetWebRequest(this.GetUri (address));

  this.DownloadBits(request, null, new CompletionDelegate (this.DownloadStringAsyncCallback), asyncOp
);
   }
   catch (Exception exception)
   {
     if (((exception is ThreadAbortException) || (exception is  StackOverflowException)) || (exception is OutOfMemoryException))
     {
       throw;
     }
     if (!(exception is WebException) && !(exception is  SecurityException))
     {
       exception = new WebException(SR.GetString("net_webclient"),  exception);
     }
     this.DownloadStringAsyncCallback(null, exception, asyncOp);
   }
   catch
   {
     Exception exception2 = new WebException(SR.GetString("net_webclient"), new  Exception(SR.GetString("net_nonClsCompliantException")));
     this.DownloadStringAsyncCallback(null, exception2, asyncOp);
   }
   if (Logging.On)
   {
     Logging.Exit(Logging.Web, this, "DownloadStringAsync", "");
   }
}

而且,使用了WebRequest的基于IAsyncResult的APM,可以看看DownloadBits的定义:

private byte[] DownloadBits(WebRequest request, Stream writeStream,  CompletionDelegate completionDelegate, AsyncOperation asyncOp)
{
   WebResponse response = null;
   DownloadBitsState state = new DownloadBitsState(request, writeStream,  completionDelegate, asyncOp, this.m_Progress, this);
   if (state.Async)
   {

request.BeginGetResponse(new AsyncCallback(WebClient.DownloadBitsResponseCallback),  state);

     return null;
   }
   response = this.m_WebResponse = this.GetWebResponse(request);
   int bytesRe

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号