时间:2021-07-01 10:21:17 帮助过:20人阅读
public class AsyncExample
{
// Synchronous methods.
public int Method1(string param);
public void Method2(double param);
// Asynchronous methods.
public void Method1Async(string param);
public void Method1Async(string param, object userState);
public event Method1CompletedEventHandler Method1Completed;
public void Method2Async(double param);
public void Method2Async(double param, object userState);
public event Method2CompletedEventHandler Method2Completed;
public void CancelAsync(object userState);
public bool IsBusy { get; }
// Class implementation not shown.
}
这里虚构的 AsyncExample 类有两个方法,都支持同步和异步调用。同步重载的行为类似于方法调用,它们对调用线程执行操作;如果操作很耗时,则调用的返回可能会有明显的延迟。异步重载将在另一个线程上启动操作,然后立即返回,允许在调用线程继续执行的同时让操作“在后台”执行。
System.Net.WebClient 本身就有很多EAP的例子,以它的DownloadString为例,WebClient中跟DownloadString相关的方法有:
DownloadString:同步下载字符串资源的方法,此方法阻塞当前线程。
DownloadStringAsync:使用EAP异步编程模式下载字符串资源的方法,此方法不会阻塞当前线程。
DownloadStringCompleted:响应异步下载时完成的事件。
DownloadProgressChanged:响应异步下载时进度变化。
调用模型示例如下:
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace AsyncTest1.EAP
{
public class EAPRunTest1
{
public static void AsyncRun() {
Utility.Log("AsyncRun:start");
//测试网址
string url = "http://sports.163.com/nba/";
using (WebClient webClient = new WebClient()) {
//监控下载进度
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
//监控完成情况
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(url));
Utility.Log("AsyncRun:download_start");
}
}
static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string log = "AsyncRun:download_completed";
log += "|cancel=" + e.Cancelled.ToString() ;
if (e.Error != null)
{
//出现异常,就记录异常
log += "|error=" + e.Error.Message;
}
else {
//没有出现异常,则记录结果
log += "|result_size=" + Utility.GetStrLen(e.Result);
}
Utility.Log(log);
}
static void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Utility.Log("AsyncRun:download_progress|percent=" + e.ProgressPercentage.ToString());
}
}
}
运行结果:
2012-12-28 00:39:39:621 AsyncRun:start
2012-12-28 00:39:40:377 AsyncRun:download_start
2012-12-28 00:39:40:903 AsyncRun:download_progress|percent=1
2012-12-28 00:39:40:933 AsyncRun:download_progress|percent=3
2012-12-28 00:39:40:933 AsyncRun:download_progress|percent=5
2012-12-28 00:39:40:934 AsyncRun:download_progress|percent=5
2012-12-28 00:39:40:975 AsyncRun:download_progress|percent=9
2012-12-28 00:39:41:068 AsyncRun:download_progress|percent=21
2012-12-28 00:39:41:131 AsyncRun:download_progress|percent=29
2012-12-28 00:39:41:182 AsyncRun:download_progress|percent=37
2012-12-28 00:39:41:298 AsyncRun:download_progress|percent=50
2012-12-28 00:39:41:354 AsyncRun:download_progress|percent=58
2012-12-28 00:39:41:447 AsyncRun:download_progress|percent=74
2012-12-28 00:39:41:489 AsyncRun:download_progress|percent=82
2012-12-28 00:39:41:582 AsyncRun:download_progress|percent=100
2012-12-28 00:39:41:582 AsyncRun:download_progress|percent=100
2012-12-28 00:39:41:614 AsyncRun:download_completed|cancel=False|result_size=205568