在.NET 4.0中,最簡單的方法是使用ParallelExtensionsExtras的AsycCache和DownloadStringTask擴展方法。事實上,example for this code涵蓋您的具體情況:
public sealed class HtmlAsyncCache : AsyncCache<Uri, string>
{
public HtmlAsyncCache() :
base(uri => new WebClient().DownloadStringTask(uri)) { }
}
...
HtmlAsyncCache cache = new HtmlAsyncCache();
var page1 = cache.GetValue(new Uri(「http://msdn.microsoft.com/pfxteam」));
var page2 = cache.GetValue(new Uri(「http://msdn.com/concurrency」));
var page3 = cache.GetValue(new Uri(「http://www.microsoft.com」));
Task.Factory.ContinueWhenAll(
new [] { page1, page2, page3 }, completedPages =>
{
… // use the downloaded pages here
});
詳情請參閱here。