AutoResetEvent配合webClient.DownloadDataAsync 接收不到信号问题

erictang2003 erictang2003 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
就是很简单的从书上抄下来的,放在wpf的 MainWindow 中测试,但是发现AutoResetEvent 接收不到 .Set() 信号,永远都是超时webClient.CancelAsync() 的情况,求测试求解答。

public MainWindow()
{
InitializeComponent();

///////////////////////////////////////////////////////

AutoResetEvent downloadWaitEvent = new AutoResetEvent(false);
WebClient webClient = new WebClient();

webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(rr);


var imgUrl = "http://img4.cache.netease.com/m/2013/12/18/20131218104403bd5df.png";//

//
webClient.DownloadDataAsync(new Uri(imgUrl), downloadWaitEvent);

if (downloadWaitEvent.WaitOne(10000)) //
{
//
System.Windows.MessageBox.Show("ll");
//
}
else
{
webClient.CancelAsync();
System.Windows.MessageBox.Show("CancelAsync");
}
}

private void rr(object sender, DownloadDataCompletedEventArgs e)
{
//
var waiterTmp = (AutoResetEvent)e.UserState;
//
try
{
//
if (!e.Cancelled && e.Error == null)
{
var bytes = e.Result;
//
}
}
finally
{
//
waiterTmp.Set(); //
//

}
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
erictang2003
C# 111,128 成员 642,536 内容