如何封装一个异步函数操作?

soddyque soddyque 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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"); 所以不好改变..
现在改变只是通知一个进度条控件的显示...
...全文
评论 3
收藏 已收藏
分享
返回顶部
soddyque
C# 111,130 成员 642,540 内容