0
我需要從不同的url下載幾個文本,然後我使用CountDownEvent來處理我的事件Donwnload完成的次數,但事情是我的CountDownEvent永遠不會設置爲零這仍然在等待。CountdownEvent從不設置爲零
任何想法這段代碼有什麼問題?
namespace WebApplication.AsyncCall
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
private CountdownEvent countDown = null;
public CountdownEvent CountDown
{
get
{
if (this.countDown == null)
{
this.countDown = new CountdownEvent(1);
}
return this.countDown;
}
}
private List<string> text = null;
public List<string> Text
{
get
{
if (this.text == null)
{
this.text = new List<string>();
}
return this.text;
}
}
protected void Page_Load(object sender, EventArgs e)
{
List<string> rssSources = new List<string>();
rssSources.Add(@"http://news.yahoo.com/rss/entertainment");
rssSources.Add(@"http://go.microsoft.com/fwlink/?linkid=84795&clcid=409");
foreach (string uri in rssSources)
{
this.CountDown.AddCount();
LoadSources(uri);
}
this.CountDown.Signal();
this.CountDown.Wait();
}
private void LoadSources(string uri)
{
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri(uri, UriKind.Absolute));
client.DownloadStringCompleted += (s, a) =>
{
if (a.Error == null && !a.Cancelled)
{
this.Text.Add(a.Result);
this.CountDown.Signal();
}
};
}
}
}
在添加偵聽器之前完成異步調用嗎?嘗試移動'client.DownloadStringAsync(new Uri(uri,UriKind.Absolute));'到'LoadSources'方法的末尾,看看它是否有效。 – Jeff
謝謝,但沒有工作! –
確定調用回調嗎?編輯:另外,你可以嘗試在while循環中查詢'CountDown'的值,而不是使用'this.CountDown.Wait();'以幫助調試。 – Jeff