我有一个C#程序,它当前同步从几个站点下载数据,之后代码对我下载的数据进行了一些处理.我试图移动它以异步方式进行下载,然后处理我下载的数据.我在测序方面遇到了一些麻烦.下面是我正在使用的代码的快照:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Started URL downloader");
UrlDownloader d = new UrlDownloader();
d.Process();
Console.WriteLine("Finished URL downloader");
Console.ReadLine();
}
}
class UrlDownloader
{
public void Process()
{
List<string> urls = new List<string>() {
"http://www.stackoverflow.com",
"http://www.microsoft.com",
"http://www.apple.com",
"http://www.google.com"
};
foreach (var url in urls)
{
WebClient Wc = new WebClient();
Wc.OpenReadCompleted += new OpenReadCompletedEventHandler(DownloadDataAsync);
Uri varUri = new Uri(url);
Wc.OpenReadAsync(varUri, url);
}
}
void DownloadDataAsync(object sender, OpenReadCompletedEventArgs e)
{
StreamReader k = new StreamReader(e.Result);
string temp = k.ReadToEnd();
PrintWebsiteTitle(temp, e.UserState as string);
}
void PrintWebsiteTitle(string temp, string source)
{
Regex reg = new Regex(@"<title[^>]*>(.*)</title[^>]*>");
string title = reg.Match(temp).Groups[1].Value;
Console.WriteLine(new string('*', 10));
Console.WriteLine("Source: {0}, Title: {1}", source, title);
Console.WriteLine(new string('*', 10));
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我的问题是这个.我的输出是:
Started URL downloader
Finished URL downloader
"Results of d.Process()"
Run Code Online (Sandbox Code Playgroud)
我想要做的是完成d.Process()方法,然后返回我的Program类中的"Main"方法.所以,我正在寻找的输出是:
Started URL downloader
"Results of d.Process()"
Finished URL downloader
Run Code Online (Sandbox Code Playgroud)
我的d.Process()方法异步运行,但我无法弄清楚如何在返回到我的Main方法之前等待所有处理完成.有关如何在C#4.0中执行此操作的任何想法?我不知道如何"告诉"我的Process()方法等到所有异步活动完成后再返回Main方法.
如果您使用.NET> = 4.0,则可以使用TPL
Parallel.ForEach(urls, url =>
{
WebClient Wc = new WebClient();
string page = Wc.DownloadString(url);
PrintWebsiteTitle(page);
});
Run Code Online (Sandbox Code Playgroud)
我也会使用HtmlAgilityPack来解析页面而不是正则表达式.
void PrintWebsiteTitle(string page)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
Console.WriteLine(doc.DocumentNode.Descendants("title").First().InnerText);
}
Run Code Online (Sandbox Code Playgroud)