我在单独的线程中执行多个HttpWebRequest,但是在我调用Start()
后我的UI线程几乎一直被阻止,我无法找出原因,这是最重要的代码:
private void Start()
{
for (int i = 0; i < 10; i++)
{
var todo = UrlStack.UrlPartitions.First();
UrlStack.UrlPartitions.Remove(todo);
ThreadPool.QueueUserWorkItem(new WaitCallback(ScanSites), todo);
}
}
private void ScanSites(object o)
{
var ToDo = (List<string>)o;
foreach (string Url in ToDo)
{
var state = MakeRequest(Url);
var website = WebsiteProcessing.ProcessResponse(state);
if (website != null)
{
Websites.Add(website);
WebsitesCount++;
this.UIThread(() => lblCrawledWebsites.Text = WebsitesCount.ToString());
}
}
}
private static RequestState MakeRequest(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
...
RequestState state = new RequestState();
HttpWebResponse response = null;
Stream responseStream = null;
StreamReader sr = null;
try
{
response = (HttpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
sr = new StreamReader(responseStream);
state.Request = (HttpWebRequest)request;
state.Response = (HttpWebResponse)response;
string strContent = sr.ReadToEnd();
state.ResponseContent = new StringBuilder(strContent);
}
catch
{
return null;
}
finally
{
if (responseStream != null)
responseStream.Close();
if (sr != null)
sr.Close();
if (response != null)
response.Close();
}
return state;
}
答案 0 :(得分:0)
我发现这个错误让我非常惊讶。这是一个自定义类,有20-30个正则表达式用于在使用HtmlAgilityPack提取文本后删除剩余的HTML,我仍然不明白背景线程如何阻止UI,CPU峰值从未超过50%。
答案 1 :(得分:0)
可能是因为这个电话?
this.UIThread(() => lblCrawledWebsites.Text = WebsitesCount.ToString());
您没有提供UIThread的定义,但看起来它正在调整对UI线程的调用。在这种情况下,如果ToDo列表中有许多URL,则可能会阻塞UI线程。