0
下面是我用來從多個url檢索數據的代碼。對於幾個網址,我會得到例外,但對於其他所有其他網站我都會收到有效的數據。問題是,在下面的應用程序中,我無法收集能夠在沒有任何問題的情況下檢索數據的線程的數據。無論如何收集儘可能多的網址迴應,同時也知道哪些網址引發異常?在可能的情況下處理異常,同時獲取數據
static void Main(string[] args)
{
var URLsToProcess = new List<string>
{
"http://www.microsoft.com",
"http://www.stackoverflow.com",
"http://www.google.com",
"http://www.apple.com",
"http://www.ebay.com",
"http://www.oracle.com",
"http://www.gmail.com",
"http://www.amazon.com",
"http://www.outlook.com",
"http://www.yahoo.com",
"http://www.amazon124.com",
"http://www.msn.com"
};
string[] tURLs = null;
try
{
tURLs = URLsToProcess
.AsParallel()
.WithDegreeOfParallelism(3)
.Select(uri => DownloadStringAsTask(new Uri(uri)).Result)
.ToArray();
}
catch (AggregateException ex)
{
AggregateException exf = ex.Flatten();
}
Console.WriteLine("download all done");
if (tURLs != null)
{
foreach (string t in tURLs)
{
Console.WriteLine(t);
}
}
}
static Task<string> DownloadStringAsTask(Uri address)
{
TaskCompletionSource<string> tcs =
new TaskCompletionSource<string>();
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, args) =>
{
if (args.Error != null)
tcs.SetException(args.Error);
else if (args.Cancelled)
tcs.SetCanceled();
else
tcs.SetResult(args.Result);
};
client.DownloadStringAsync(address);
return tcs.Task;
}
不知道我是否應該發佈一個新問題,但是您可以更改您的示例,以便我開始取得進展,哪些任務已完成,而其他任務仍在進行中,並且有些已引發異常? – BKS
@johnsmith你應該發表一個不同的問題。你可以在這裏鏈接它,我會看看。 – i3arnon
好的,會做到這一點。儘管感謝您的幫助。 – BKS