gpt4 book ai didi

c# - 使用Xpcom.Shutdown(),Xpcom.Initialize(path)后的GeckoFx不能再使用

转载 作者:行者123 更新时间:2023-12-03 13:12:47 30 4
gpt4 key购买 nike

我想在我的C#应用​​程序中使用一个以上的壁虎浏览器。

所以我做了什么:

FORM 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).");
}
}

最佳答案

看来这仍然是一个问题,在单元测试中非常重要。运行所有单元测试(从初始化开始),一切都很好。尝试再次运行它们,根据xpCom的状态,您可能会遇到问题,因为无法关闭。

类似的问题在这里:Using GeckoFX in a multithreaded application

关于c# - 使用Xpcom.Shutdown(),Xpcom.Initialize(path)后的GeckoFx不能再使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20813582/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com