我正在用C#编写一个应用程序,并且我创建了多个BackgroundWorker线程来从网页中获取信息。尽管他们是BackgroundWorkers,但我的GUI表单变得没有反应。在Main中处理Web请求?
当我调试时,我暂停时,程序无响应,我可以看到我在主线程,我暂停了网页抓取方法。这个方法只能从新线程调用,所以我不知道为什么我会在主线程中。
这是否有意义?我可以做些什么来确保Web请求只能在各自的线程中处理?
编辑:一些代码和解释
我处理地址的大名单。每个线程将处理一个或多个地址。我可以选择我想要多少个线程创建(我把它谦虚:))
//in “Controller” class
public void process()
{
for (int i = 1; i <= addressList.Count && i<= numthreads; i++)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += doWork;
bw.RunWorkerAsync((object)i);
}
}
public void doWork(object sender, DoWorkEventArgs e)
{
//create an object that has the web fetching method, call it WorkObject
//WorkObject keeps a reference to Controller.
//When it is done getting information, it will send it to Controller to print
//generate a smaller list of addresses to work on, using e.Argument (should be 'i' from the above 'for' loop)
WorkObject.workingMethod()
}
当创建工作对象,它使用“我”就知道它是什么的线程数。它将使用它来获取网站地址列表(从主窗体,控制器和每个WorkObjects共享的更大地址列表中获取信息 - 每个线程将处理更小的地址列表)。当它遍历列表时,它会调用“getWebInfo”方法。
//in “WorkObject” class
public static WebRequest request;
public void workingMethod()
{
//iterate over the small list of addresses. For each one,
getWebInfo(address)
//process the info a bit...then
myController.print()
//note that this isn’t a simple “for” loop, it involves event handlers and threading
//Timers to make sure one is done before going on to the next
}
public string getWebInfo (string address)
{
request = WebRequest.Create(address);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
return content;
}
你能发表一些代码吗?确定启动线程的代码和请求 – mfeingold 2009-11-24 20:32:13
。希望这是足够的/不太多的信息。 – Matt 2009-11-24 21:36:17