我在 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;
}