Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GeckoFx在使用Xpcom.Shutdown()之后,Xpcom.Initialize(path)不能再使用了_C#_Multithreading_Winforms_Geckofx - Fatal编程技术网

C# GeckoFx在使用Xpcom.Shutdown()之后,Xpcom.Initialize(path)不能再使用了

C# GeckoFx在使用Xpcom.Shutdown()之后,Xpcom.Initialize(path)不能再使用了,c#,multithreading,winforms,geckofx,C#,Multithreading,Winforms,Geckofx,我希望在我的C#应用程序中有不止一个gecko浏览器 所以我所做的是: 表格用户界面代码: public partial class GeckoBrowserForm : Form { static GeckoBrowserForm() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(fa

我希望在我的C#应用程序中有不止一个gecko浏览器

所以我所做的是:

表格用户界面代码:

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(路径)更多。程序刚刚停止,没有任何错误

为什么?

我知道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的状态,因为无法关闭


这里也有类似的问题:

Xpcom.Shutdown();不会撤消Xpcom.Initialize执行的所有本机初始化。(例如,存在于xul.dll中的全局变量)。如果您可以在调用Xpcom.Shutdown后强制卸载firefox/xulrunner DLL(FreeLibrary?),这可能会重置进程中的本机内存空间,并允许另一个Xpcom.Initialize调用。您找到解决方案了吗?我也面临同样的问题。