0

我在 Seaprate 线程上执行多个 HttpWebRequests,但我的 UI 线程在我调用后几乎一直被阻塞Start(),我不知道为什么,这是最重要的代码:

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;
}
4

2 回答 2

0

我发现了让我非常吃惊的故障。这是一个带有 20-30 个正则表达式的自定义类,用于在使用 HtmlAgilityPack 提取文本后剥离剩余的 HTML,我仍然不明白的是后台线程如何阻止 UI,CPU 峰值从未超过 50%。

于 2012-08-13T12:33:12.660 回答
0

可能是因为这个电话?

this.UIThread(() => lblCrawledWebsites.Text = WebsitesCount.ToString()); 

您没有提供 UIThread 的定义,但看起来它正在编组对 UI 线程的调用。在这种情况下,如果 ToDo 列表中有很多 URL,它可能会阻塞 UI 线程。

于 2012-08-13T16:47:06.747 回答