GeckoFx只能从初始化它的同一个线程中调用(通常是UI线程)

时间:2013-12-13 08:29:19

标签: c# multithreading gecko geckofx

GeckoBrowserForm clasc

    protected override void OnLoad(EventArgs e)
            {
        m_GeckoWebBrowser.Parent = this;
                        m_GeckoWebBrowser.Dock = DockStyle.Fill;
                        m_GeckoWebBrowser.DocumentCompleted += (s, ee) =>
                        {
                            GeckoHtmlElement element = null;
                            var geckoDomElement = m_GeckoWebBrowser.Document.DocumentElement;
                            if (geckoDomElement != null && geckoDomElement is 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);
    }

private static void InitializeXulRunner(string path)
        {
            if (s_IsXulrunnerInitialized)
            {
                return;
            }

            s_IsXulrunnerInitialized = true;
            Xpcom.Initialize(path);
        }

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();
        }

和其他课程我打电话:

using (GeckoBrowserForm geckoBrowserForm = new GeckoBrowserForm(XulRunnerPath, propertyBag.ResponseUri.ToString()))
            {
                //geckoBrowserForm.CreateControl();
                geckoBrowserForm.Show();
                while (!geckoBrowserForm.Done)
                {
                    Application.DoEvents();
                }

                propertyBag.GetResponse = () => new MemoryStream(Encoding.UTF8.GetBytes(geckoBrowserForm.DocumentDomHtml));
                //geckoBrowserForm.Dispose();
                base.Process(crawler, propertyBag);
            }

但我总是得到

  

GeckoFx只能从它所在的同一个线程中调用   初始化(通常是UI线程)。

m_GeckoWebBrowser

中的

我该怎么办? Ir正在使用旧的Skybound.Gecko,但不使用新的GeckoFx

1 个答案:

答案 0 :(得分:0)

Geckofx不支持multithread,因为正如您提到的那样,它正在处理UI线程。但您可以通过以下代码尝试:

this.BeginInvoke(new Action(() => {
 //What you want gecko browser to do! Like:
   using (GeckoBrowserForm geckoBrowserForm = new GeckoBrowserForm(XulRunnerPath, propertyBag.ResponseUri.ToString()))
        {
            //geckoBrowserForm.CreateControl();
            geckoBrowserForm.Show();
            while (!geckoBrowserForm.Done)
            {
                Application.DoEvents();
            }

            propertyBag.GetResponse = () => new MemoryStream(Encoding.UTF8.GetBytes(geckoBrowserForm.DocumentDomHtml));
            //geckoBrowserForm.Dispose();
            base.Process(crawler, propertyBag);
        }
}));