如何封装一个异步函数操作?
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"); 所以不好改变..
现在改变只是通知一个进度条控件的显示...
{
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"); 所以不好改变..
现在改变只是通知一个进度条控件的显示...
...全文
相关推荐
微信小程序 request接口的封装实例代码
主要介绍了微信小程序 request接口的封装实例代码的相关资料,需要的朋友可以参考下
jQuery Ajax 全局调用封装实例代码详解
主要介绍了jQuery Ajax 全局调用封装实例代码详解的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
alert、confirm封装
封装了alert和confirm,不用原版alert和confirm。
node基于async/await对mysql进行封装
主要介绍了node基于async/await对mysql进行封装,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
Javascript中的匿名函数与封装介绍
主要介绍了Javascript中的匿名函数与封装介绍,本文分析了jQuery 封装、Backbone 封装、Underscore 封装等内容,需要的朋友可以参考下


