C# GeckoFx只能从初始化它的同一线程(通常是UI线程)调用。SynchronizationContext不工作

C# GeckoFx只能从初始化它的同一线程(通常是UI线程)调用。SynchronizationContext不工作,c#,multithreading,c#-4.0,user-interface,geckofx,C#,Multithreading,C# 4.0,User Interface,Geckofx,这是我的班壁虎 namespace NCrawler.GeckoProcessor { public partial class GeckoBrowserForm : Form { #region Readonly & Static Fields private static bool s_IsXulrunnerInitialized; private readonly GeckoWebBrowser m_GeckoWeb

这是我的班壁虎

namespace NCrawler.GeckoProcessor
{
    public partial class GeckoBrowserForm : Form
    {
        #region Readonly & Static Fields

        private static bool s_IsXulrunnerInitialized;
        private readonly GeckoWebBrowser m_GeckoWebBrowser = new GeckoWebBrowser();
        private readonly string m_Url;
        private SynchronizationContext m_uiContext;

        #endregion

        #region Constructors

        static GeckoBrowserForm()
        {   
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
        }

        public GeckoBrowserForm(string xulRunnerPath, string url)
        {
            m_uiContext = SynchronizationContext.Current;
            if (m_uiContext == null)
            {
                m_uiContext = new SynchronizationContext();
            }

            InitializeXulRunner(xulRunnerPath);
            m_Url = url;
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.Manual;
            Location = new Point(0, 0);
            Size = new Size(800, 800);
            Done = false;
            InitializeComponent();
        }

        #endregion

        #region Instance Properties

        public string DocumentDomHtml { get; set; }
        public Boolean Done { get; set; }

        #endregion

        #region Instance Methods

        protected override void OnLoad(EventArgs e)
        {   
            Thread thread = new Thread(new ThreadStart(DoWork));
            thread.Start();
        }

        private void DoWork()
        {
            // lets see the thread id
            int id = Thread.CurrentThread.ManagedThreadId;
            Trace.WriteLine("Run thread: " + id);

            m_uiContext.Send(new SendOrPostCallback(_ =>
            {
                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;
                    }
                };

                m_GeckoWebBrowser.Navigate(m_Url);
            }), null);
        }

        #endregion

        #region Class Methods

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

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

        #endregion
    }
}
但是
DoWork
方法得到了一个例外

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

我已经读到我需要使用
SynchronizationContext
,但它是一样的。还是一样的错误。我正在其他项目中与

GeckoBrowserForm geckoBrowserForm = new GeckoBrowserForm(XulRunnerPath, Uri.ToString()))
geckoBrowserForm.Show();
而且它在线程内部,所以我希望有
5个浏览器


我能做什么?
SynchronizationContext
实现是否正确?

浏览器与任何大型软件一样,都不是线程安全的。因此,不要使用线程来获得成功。不需要它们,Navigate()调用只需几毫秒。然后它在背景中滚动了一段时间。并让您知道这是通过触发DocumentCompleted事件来完成的。由于性能原因,我需要更多线程。所有“new GeckoBrowserForm(..)”调用都在同一线程上完成?您的主入口点是否具有STAThread属性?Tom否“new GeckoBrowserForm(..)不是在同一线程上完成的,我认为这是个问题。是的,我在线程上设置了STAThread