在不同的线程运行与控制浏览器控件 [英] Run and control browser control in different thread
问题描述
我有一些subclassess我的主GUI类。有+ - 。3线程正在收集各种网络资源和API网关等数据。
I have my main gui class with some subclassess. There are +- 3 threads that are collecting data from various internet sources and API gateways etc.
现在,这些线程之一,我想运行一个WebBrowser控件,这样我就可以添加一些autobrowsing功能,我的计划。每个子线程应该能够打开自身一个网页浏览器的。所以,我创建了一个第二个C#窗口形式,它仅包含控制网页浏览
Now, out of one of these threads, I want to run a webbrowser control, so I can add some autobrowsing functionality to my program. Each of the sub threads should be capable of opening a webbrowser on its own. So I created a second c# windows form, which contains only the webbrowsing control.
我已经使用ApartmentState.STA上WebBrowser控件这个新的线程设置。不过,该窗体2没有响应
I already use the ApartmentState.STA setting on this new thread for the webbrowser control. However, the form2 is unresponsive.
我试着打电话给Application.Run();从这个线程,这使得web浏览器/窗体2响应。但后来我的主线程停止运行。
I tried to call Application.Run(); from this thread, and this makes the webbrowser/form2 responsive. But then my main thread stops running.
所以,我在如何进行一个有点不确定。是我想在所有可能的?
So I'm a bit unsure on how to proceed. Is what I want possible at all ?
推荐答案
这应该工作
var th = new Thread(() =>
{
WebBrowserDocumentCompletedEventHandler completed = null;
using (WebBrowser wb = new WebBrowser())
{
completed = (sndr, e) =>
{
//Do Some work
wb.DocumentCompleted -= completed;
Application.ExitThread();
};
wb.DocumentCompleted += completed;
wb.Navigate(url);
Application.Run();
}
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join();
这是说,我会用的 WebClient的或的 HttpWebRequest的连同 HtmlAgilityPack 下载和解析HTML资源
that said, I would use WebClient or HttpWebRequest together with HtmlAgilityPack to download and parse html resources
这篇关于在不同的线程运行与控制浏览器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!