20.1 什么是异步编程异步编程就是把耗时的操作放进一个单独的线程中进行处理。
20.2 同步方式存在的问题
- namespace 异步编程
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- txbUrl.Text = "http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe";
- }
-
- private void btnDownLoad_Click(object sender, EventArgs e)
- {
- rtbState.Text = "下载中.....";
- if (txbUrl.Text == string.Empty)
- {
- MessageBox.Show("请先输入下载地址!");
- return;
- }
- DownLoadFileSync(txbUrl.Text.Trim());
- }
-
- public void DownLoadFileSync(string url)
- {
- int BufferSize = 2084;
- byte[] BufferRead = new byte[BufferSize];
- string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\我的QQ.exe";
- FileStream fileStream = null;
- HttpWebResponse myWebResponse = null;
- if (File.Exists(savePath))
- {
- File.Delete(savePath);
- }
- fileStream = new FileStream(savePath, FileMode.OpenOrCreate);
-
- try
- {
- HttpWebRequest myHTTpWebRequest = (HttpWebRequest) WebRequest.Create(url);
- if (myHTTpWebRequest != null)
- {
- myWebResponse = (HttpWebResponse) myHTTpWebRequest.GetResponse();
- Stream responseStream = myWebResponse.GetResponseStream();
- int readSize = responseStream.Read(BufferRead, 0, BufferSize);
- while (readSize > 0)
- {
- fileStream.Write(BufferRead, 0, readSize);
- readSize = responseStream.Read(BufferRead, 0, BufferSize);
- }
- rtbState.Text = "文件下载完成,文件大小为:" + fileStream.Length + "下载路径为:" + savePath;
- }
- }
- catch (Exception ex)
- {
- rtbState.Text = "下载异常,异常信息为:" + ex.Message;
- }
- finally
- {
- if (myWebResponse != null)
- {
- myWebResponse.Close();
-
- }
- if (fileStream !=null)
- {
- fileStream.Close();
- }
- }
- }
- }
- }
虽然完成了文件下载功能,但却带来不好的用户体验,
点“下载”按钮后,程序处于“假死”状态。而且,“下载中....”也没有显示在界面中。
主线程忙于执行下载操作,无法空出时间来绘制窗体,导致在下载过程中出现“假死”现象。
异步编程可以很好的解决这个问题。
20.3 异步编程模型——APM(.NET不推荐)
APM 是
Asynchronous Programming Mode (异步编程模式)的缩写。(
在.NET Framework 4 和更早的版本中)
它允许程序用更少的线程去执行更多的操作。
要分辨某个类是否实现异步编程模式,主要看该类是否实现了返回类型为 IAsyncResult 接口的 Beginxxx 和 Endxxx 方法。
1.Beginxxx 方法——开始执行异步操作
在要获取文件中内容时,通常使用
FileStream 的同步方法
Read 进行读取,定义:
public abstract int Read(byte[] array, int offset, int count); |
该方法从文件流中读取字节块,然后将该数据写入给定的字节数组
array 中。这里 array 表示读取的字节块要被写入的地方, offset 表示从文件流读取字节的开始位置, count 表示最多读取的字节数。
读取大文件时,调用 Read 方法会阻塞UI线程,导致文件没有读取前窗体无法响应。为了解决这个情况,.NET1.0就提出了异步编程模型,并为 FileStream 类提供了异步模型方法,即 BeginRead 方法。该方法会异步执行读取,并返回 IAsyncResult 接口的对象(该对象存储异步操作信息)。
public virtual IAsyncResult BeginRead(
byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state
) |
该方法前面3个参数和同步方法 Read 一致,后两个参数 callback 和 state 则不同。
callbackType:
System.AsyncCallback
异步操作完成后需要回调的方法,该方法必须匹配 AsyncCallback委托类型。
stateType:
System.Object
传递给回调方法的对象,在回调方法中,可以查询 IAsyncResult 接口的 AsyncState 属性来读取该对象。
2.Endxxx 方法 ——结束异步操作
调用完 Beginxxx 方法后,还需要调用
Endxxx
方法来获取操作返回的结果。
Endxxx 返回的类型与同步方法相同,如
FileStream
的
EndRead
方法会返回一个 Int32 类型,代表从文件流中实际读取的字节数。
APM给出了4种方式来访问异步操作所得到的结果。
-
在调用 Beginxxx 方法的线程上调用 Endxxx 方法来得到异步操作的结果。然而这种方式会阻塞调用线程,使其一直挂起,直至操作完成。
-
在调用 Beginxxx 方法的线程查询 IAsyncResult 的 AsyncWaitHandle 属性,从而得到 WaitHandle 对象,接着调用该对象 WaitOne 方法来堵塞线程并等待操作完成,最后调用 Endxxx 方法获得操作结果。
-
在调用 Beginxxx 方法的线程上循环查询 IAsyncResult 的 IsComplete 属性,操作完成后调用 Endxxx 方法获得操作结果。
-
使用 AsyncCallback 委托来指定操作完成要调用的方法,在回调方法中调用 Endxxx 方法来获得异步操作返回的结果。
上面4种,前3种会堵塞调用线程。
第1种方式:
-
public void DownLoadFileSync(string url) -
{ -
int BufferSize = 2084; -
byte[] BufferRead = new byte[BufferSize]; -
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\我的QQ.exe"; -
FileStream fileStream = null; -
HttpWebResponse myWebResponse = null; -
if (File.Exists(savePath)) -
{ -
File.Delete(savePath); -
} -
fileStream = new FileStream(savePath, FileMode.OpenOrCreate); -
-
try -
{ -
HttpWebRequest myHTTpWebRequest = (HttpWebRequest) WebRequest.Create(url); -
if (myHTTpWebRequest != null) -
{ -
-
IAsyncResult result = myHTTpWebRequest.BeginGetResponse(null, null); -
myWebResponse = (HttpWebResponse)myHTTpWebRequest.EndGetResponse(result); -
-
Stream responseStream = myWebResponse.GetResponseStream(); -
int readSize = responseStream.Read(BufferRead, 0, BufferSize); -
while (readSize > 0) -
{ -
-
fileStream.Write(BufferRead, 0, readSize); -
readSize = responseStream.Read(BufferRead, 0, BufferSize); -
} -
rtbState.Text = "文件下载完成,文件大小为:" + fileStream.Length + "下载路径为:" + savePath; -
} -
} -
catch (Exception ex) -
{ -
rtbState.Text = "下载异常,异常信息为:" + ex.Message; -
} -
finally -
{ -
if (myWebResponse != null) -
{ -
myWebResponse.Close(); -
-
} -
if (fileStream !=null) -
{ -
fileStream.Close(); -
} -
} -
}
|
DownLoadFileSync 方法通过调用 BeginGetResponse 方法来异步请求资源,执行该方法后立即返回UI线程。UI线程继续执行代码,遇到 EndGetResponse 方法,此方法堵塞UI线程,使得效果和同步方法实现一样。
-
namespace 异步编程 -
{ -
public partial class Form1 : Form -
{ -
public Form1() -
{ -
InitializeComponent(); -
txbUrl.Text = "http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe"; -
} -
-
private delegate string AsyncMethodCaller(string fileurl); -
-
private SynchronizationContext sc; -
-
private void btnDownLoad_Click(object sender, EventArgs e) -
{ -
rtbState.Text = "下载中....."; -
btnDownLoad.Enabled = false; -
if (txbUrl.Text == string.Empty) -
{ -
MessageBox.Show("请先输入下载地址!"); -
return; -
} -
-
sc = SynchronizationContext.Current; -
AsyncMethodCaller methodCaller = new AsyncMethodCaller(DownLoadFileSync); -
methodCaller.BeginInvoke(txbUrl.Text.Trim(), GetResult, null); -
} -
-
private void GetResult(IAsyncResult result) -
{ -
AsyncMethodCaller caller = (AsyncMethodCaller)((AsyncResult)result).AsyncDelegate; -
string returning = caller.EndInvoke(result); -
sc.Post(ShowState, returning); -
} -
-
private void ShowState(object result) -
{ -
rtbState.Text = result.ToString(); -
btnDownLoad.Enabled = true; -
} -
-
public string DownLoadFileSync(string url) -
{ -
int BufferSize = 2084; -
byte[] BufferRead = new byte[BufferSize]; -
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\我的QQ.exe"; -
FileStream fileStream = null; -
HttpWebResponse myWebResponse = null; -
if (File.Exists(savePath)) -
{ -
File.Delete(savePath); -
} -
fileStream = new FileStream(savePath, FileMode.OpenOrCreate); -
-
try -
{ -
HttpWebRequest myHTTpWebRequest = (HttpWebRequest)WebRequest.Create(url); -
if (myHTTpWebRequest != null) -
{ -
myWebResponse = (HttpWebResponse)myHTTpWebRequest.GetResponse(); -
Stream responseStream = myWebResponse.GetResponseStream(); -
int readSize = responseStream.Read(BufferRead, 0, BufferSize); -
while (readSize > 0) -
{ -
-
fileStream.Write(BufferRead, 0, readSize); -
readSize = responseStream.Read(BufferRead, 0, BufferSize); -
} -
} -
return string.Format("文件下载完成,文件大小为:" + fileStream.Length + "下载路径为:" + savePath); -
} -
catch (Exception ex) -
{ -
return string.Format("下载异常,异常信息为:" + ex.Message); -
} -
finally -
{ -
if (myWebResponse != null) -
{ -
myWebResponse.Close(); -
-
} -
if (fileStream != null) -
{ -
fileStream.Close(); -
} -
} -
} -
} -
}
|
20.4 异步编程模型——EAP(.NET不推荐)
APM同样存在一些明显的问题,如不支持对异步操作的取消以及不能提供下载进度报告等。
微软.NET2.0发布提出了一个新的异步编程模式——基于事件的异步模式,EAP(Event-based Asynchronous Pattern)。
实现EPA的类具有一个或多个以 Async 为后缀的方法,以及对应的 Completed 事件,并且这些类支持异步方法的取消和进度报告。在.NET类库中只有部分类实现了EPA,共17个。
System.Object的派生类型: System.Activies.WorkflowInvoke System.Deployment.Application.ApplicationDeployment System.Deployment.Application.InPlaceHosingManager System.Net.Mail.SmtpClient System.Net.PeerToPeer.PeerNameResolver System.Net.PeerToPeer.Collaboration.ContactManager System.Net.PeerToPeer.Collaboration.Peer System.Net.PeerToPeer.Collaboration.PeerContact System.Net.PeerToPeer.Collaboration.PeerNearMe System.ServiceModel.Activities.WorkflowControlClient System.ServiceModel.Discovery.AnnoucementClient System.ServiceModel.Discovery.DiscoveryClient System.ComponentModel.Component的派生类型: System.ComponentModel.BackgroundWorker System.Media.SoundPlay System.Net.WebClient System.Net.NetworkInformation.Ping System.Windows.Forms.PictureBox(继承于Control类,Control类派生于Component类) |
使用最多的莫过于
BackgroundWorker
类了,该类经常被用于实现网络文件下载功能。
BackgroundWorker类 | |
公共属性 | |
属性名 | 说明 |
CancellationPending | 获取一个值,指示应用程序是否已请求取消后台操作 |
IsBusy | 获取一个值,指示 BackgroundWorker 是否正在运行异步操作。 |
WorkReportsProgress | 获取或设置一个值,该值指示 BackgroundWorker 能否报告进度更新。 |
WorkerSupportsCancellation | 获取或设置一个值,该值指示 BackgroundWorker 是否支持异步取消。 |
公共方法 | |
名称 | 说明 |
CancelAsync | 请求取消挂起的后台操作。 |
ReportProgress | 引发 ProgressChanged 事件(官方这样解释我就要信?) |
RunWorkerAsync | 开始执行后台操作。 |
公共事件 | |
名称 | 说明 |
DoWork | 调用 RunWorkerAsync 时发生(官方是这么解释的,你想知道为什么调用RunWorkerAsync方法就会触发DoWork事件吗?) |
ProgressChanged | 调用ReportProgress时发生(官方是这么解释的,你想知道为什么调用ReportProgress方法就会触发ProgressChanged事件吗?) |
RunWorkerCompleted | 当后台操作已完成、被取消或引发异常时发生。
|
-
public partial class Form1 : Form -
{ -
public int DownloadSize = 0; -
public string downloadPath = null; -
long totalSize = 0; -
const int BufferSize = 2048; -
byte[] BufferRead = new byte[BufferSize]; -
FileStream filestream = null; -
HttpWebResponse myWebResponse = null; -
-
public Form1() -
{ -
InitializeComponent(); -
string url = "http://dldir1.qq.com/qqfile/qq/QQ8.7/19113/QQ8.7.exe"; -
-
txbUrl.Text = url; -
this.btnPause.Enabled = false; -
-
GetTotalSize(); -
downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + Path.GetFileName(this.txbUrl.Text.Trim()); -
if (File.Exists(downloadPath)) -
{ -
FileInfo fileInfo = new FileInfo(downloadPath); -
DownloadSize = (int)fileInfo.Length; -
progressBar1.Value = (int)((float)DownloadSize / (float)totalSize * 100); -
} -
-
-
bgWorkerFileDownload.WorkerReportsProgress = true; -
bgWorkerFileDownload.WorkerSupportsCancellation = true; -
} -
-
private void btnDownLoad_Click(object sender, EventArgs e) -
{ -
if (bgWorkerFileDownload.IsBusy != true) -
{ -
bgWorkerFileDownload.RunWorkerAsync(); -
-
filestream = new FileStream(downloadPath, FileMode.OpenOrCreate); -
-
filestream.Seek(DownloadSize, SeekOrigin.Begin); -
this.btnDownLoad.Enabled = false; -
this.btnPause.Enabled = true; -
} -
else -
{ -
MessageBox.Show("正在执行操作,请稍后"); -
} -
} -
-
private void btnPause_Click(object sender, EventArgs e) -
{ -
if (bgWorkerFileDownload.IsBusy && bgWorkerFileDownload.WorkerSupportsCancellation == true) -
{ -
bgWorkerFileDownload.CancelAsync(); -
} -
} -
-
private void bgWorkerFileDownload_DoWork(object sender, DoWorkEventArgs e) -
{ -
BackgroundWorker bgworker = sender as BackgroundWorker; -
try -
{ -
-
-
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(txbUrl.Text.Trim()); -
-
-
-
if (DownloadSize != 0) -
{ -
myHttpWebRequest.AddRange(DownloadSize); -
} -
-
-
myWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); -
Stream responseStream = myWebResponse.GetResponseStream(); -
int readSize = 0; -
while (true) -
{ -
if (bgworker.CancellationPending == true) -
{ -
e.Cancel = true; -
break; -
} -
-
readSize = responseStream.Read(BufferRead, 0, BufferRead.Length); -
if (readSize > 0) -
{ -
DownloadSize += readSize; -
int percentComplete = (int)((float)DownloadSize / (float)totalSize * 100); -
filestream.Write(BufferRead, 0, readSize); -
-
-
bgworker.ReportProgress(percentComplete); -
} -
else -
{ -
break; -
} -
} -
} -
catch -
{ -
throw; -
} -
} -
-
private void bgWorkerFileDownload_ProgressChanged(object sender, ProgressChangedEventArgs e) -
{ -
this.progressBar1.Value = e.ProgressPercentage; -
} -
-
private void bgWorkerFileDownload_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) -
{ -
if (e.Error != null) -
{ -
MessageBox.Show(e.Error.Message); -
myWebResponse.Close(); -
} -
else if (e.Cancelled) -
{ -
MessageBox.Show(String.Format("下载暂停,下载的文件地址为:{0}\n 已经下载的字节数为: {1}字节", downloadPath, DownloadSize)); -
myWebResponse.Close(); -
filestream.Close(); -
filestream.Close(); -
-
this.btnDownLoad.Enabled = true; -
this.btnPause.Enabled = false; -
} -
else -
{ -
MessageBox.Show(String.Format("下载已完成,下载的文件地址为:{0},文件的总字节数为: {1}字节", downloadPath, totalSize)); -
-
this.btnDownLoad.Enabled = false; -
this.btnPause.Enabled = false; -
myWebResponse.Close(); -
filestream.Close(); -
} -
} -
-
private void GetTotalSize() -
{ -
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(txbUrl.Text.Trim()); -
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse(); -
totalSize = response.ContentLength; -
response.Close(); -
} -
}
|
[你必须知道的异步编程]——基于事件的异步编程模式
http://www.cnblogs.com/zhili/archive/2013/05/11/EAP.html
20.5 异步编程模型——TAP
前2种模式在.NET中被标识为不推荐使用的实现方式。
在.NET 4.0中,微软提供更简单的异步方式——基于任务的异步模式,即TAP。
该模式主要使用 System.Threading.Tasks.Task和Task<T> 类来完成异步编程,相对于前面两种异步模式来讲,TAP使异步编程模式更加简单(因为这里我们只需要关注 Task 这个类的使用),同时TAP也是微软推荐使用的异步编程模式。
基于任务的异步模式,只使用一个方法就能表示异步操作的开始和完成。只需要一个以
TaskAsync 为后缀的方法,通过向该方法传入
CancellationToken 参数,就可以完成异步编程了。而且还可以通过
IProgress<T> 接口来实现进度报告的功能。
20.6 C#5.0 中的 async 和 await
太大段代码,还是看看《C#本质论》18章 和《精通C#(第6版)》19章