private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//list of tasks
}
private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//list of tasks
}
见上文。 我的问题是,如何使它们两个同时运行(使两个线程手动控制它们的监视),现在问题是它们都在主线程上单独运行,我可以做其他任务,谢谢你的帮助!抱歉英文不好
我试图使用,例如:
Thread threadw1 = new Thread(() => webBrowser1.Navigate("url1"));
Thread threadw2 = new Thread(() => webBrowser2.Navigate("url2"));
threadw1.Start();
threadw2.Start();
但是这是使用线程导航,一旦导航完成,系统就会单独运行文档。 而且我不知道如何控制它们,谢谢你的帮助!
我已将任务包装在一个函数中,现在的问题是 当我这样做时,它不起作用
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState < WebBrowserReadyState.Complete) return;
// Do the work, you have the event args in ev
Thread wbb1 = new Thread(() => wb1task());
wbb1.Start();
//wb1task();
}
但是当我这样做时它会起作用,但它再次成为一个单一的线程:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState < WebBrowserReadyState.Complete) return;
// Do the work, you have the event args in ev
//Thread wbb1 = new Thread(() => wb1task());
// wbb1.Start();
wb1task();
}
线程启动然后文档完成,我需要在wbb1.Start()之后保留一些东西;防止它走出循环:)
好的,这解决了这个问题,谢谢大家!
wbb1.SetApartmentState(ApartmentState.STA);
答案 0 :(得分:0)
将您的任务放在DocumentCompleted
事件的处理程序中运行。我用了一个帖子,因为你提到了它。我建议使用ThreadPool
或Task
。
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
new Thread((state) => {
WebBrowserDocumentCompletedEventArgs ev =
(WebBrowserDocumentCompletedEventArgs) state;
// Do the work, you have the event args in ev
}) {Name = "Thread for webBrowser1"}.Start(e);
}