如何封装一个异步函数操作?
private string GetPage(string url)
{
progressBar1.Value = 0;
WebClient wC = new WebClient();
wC.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wC_DownloadDataCompleted);
wC.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wC_DownloadProgressChanged);
wC.DownloadDataAsync(new Uri(url));
}
void wC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void wC_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Mz(System.Text.Encoding.Unicode.GetString(e.Result));
}
===============================
这段代码就是想让GetPage返回string,而不是通过DownloadDataCompleted重新调用.
因为以前写的,有很多地方使用了诸如 string cont = GetPage("http://e.com/e.txt"); 所以不好改变..
现在改变只是通知一个进度条控件的显示...