我希望在我的C#应用程序中有多个壁虎浏览器。
所以我做了什么:
表单UI代码:
public partial class GeckoBrowserForm : Form
{
static GeckoBrowserForm()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
}
public GeckoBrowserForm(string xulRunnerPath, string url)
{
InitializeXulRunner(xulRunnerPath);
m_Url = url;
FormBorderStyle = FormBorderStyle.FixedToolWindow;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
Location = new Point(0, 0);
Size = new Size(800, 800);
Done = false;
InitializeComponent();
}
private static void InitializeXulRunner(string path)
{
if (!Xpcom.IsInitialized)
{
Xpcom.Initialize(path);
}
}
protected override void OnLoad(EventArgs e)
{
m_GeckoWebBrowser.Parent = this;
m_GeckoWebBrowser.Dock = DockStyle.Fill;
m_GeckoWebBrowser.DocumentCompleted += (s, ee) =>
{
var geckoDomElement = m_GeckoWebBrowser.Document.DocumentElement;
if (geckoDomElement is GeckoHtmlElement)
{
GeckoHtmlElement element = (GeckoHtmlElement)geckoDomElement;
DocumentDomHtml = element.InnerHtml;
}
if (m_Url.Equals(m_GeckoWebBrowser.Document.Url.ToString(), StringComparison.OrdinalIgnoreCase))
{
Done = true;
}
};
m_GeckoWebBrowser.Navigate(m_Url);
}在工作线程中,而不是UI中:
if (Xpcom.IsInitialized)
{
Xpcom.Shutdown();
}
using (GeckoBrowserForm geckoBrowserForm = new GeckoBrowserForm(XulRunnerPath, propertyBag.ResponseUri.ToString()))
{
geckoBrowserForm.Show();
while (!geckoBrowserForm.Done)
{
Application.DoEvents();
}
string html = geckoBrowserForm.DocumentDomHtml;
if (!string.IsNullOrEmpty(html))
{
propertyBag.GetResponse = () => new MemoryStream(Encoding.UTF8.GetBytes(html));
}
base.Process(crawler, propertyBag);
}问题是,在我使用Xpcom.Shutdown();之后,我不能再使用Xpcom.Initialize(path);了。程序刚刚停止,没有任何错误。
为什么?
我知道什么geckoFx只能在相同的UI线程中使用,所以我想再次初始化它
public static void AssertCorrectThread()
{
if (Thread.CurrentThread.ManagedThreadId != _XpcomThreadId)
{
throw new InvalidOperationException("GeckoFx can only be called from the same thread on which it was initialized (normally the UI thread).");
}
}发布于 2014-04-15 01:14:15
这似乎仍然是一个问题,这在单元测试时非常重要。运行所有单元测试(从初始化开始),一切都很好。再次尝试运行它们,您可能会遇到问题,这取决于xpCom的状态,因为没有办法关闭。
https://stackoverflow.com/questions/20813582
复制相似问题