此测试代码在Windows中按预期工作:
var urls5 = urls.Take(100);
var elapsed5 = Test<WebClient>(urls5);
Console.WriteLine("Webclient:" + elapsed5);
使用该方法(原谅Console.WriteLines):
static TimeSpan Test<T>(IEnumerable<string> urls) where T : WebClient, new()
{
Console.WriteLine("1");
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
Console.WriteLine("2");
var tasks = new List<Task>();
foreach (var u in urls)
{
tasks.Add(Task.Run(() =>
{
Console.WriteLine("3");
using (var wc = new T())
{
Console.WriteLine("4");
var str = wc.DownloadString(u);
Console.WriteLine("5");
var hap = new HtmlAgilityPack.HtmlDocument();
hap.LoadHtml(str);
var asdf = hap.DocumentNode.InnerText + " ";
Console.Write(".");
}
}));
}
Task.WaitAll(tasks.ToArray());
sw.Stop();
return sw.Elapsed;
}
然而在单声道(windows和linux)中,从写作线来看,每个任务只能达到“4”而不会超出,抛出超时异常。没有任务,测试代码按预期在单声道上工作。我做错了什么,或者我无意中偶然发现了一个错误?
有趣的是,如果我在XamarinStudio的调试模式下运行代码,它会收到下载的内容。