开发者

Using GeckoFX in a multithreaded application

开发者 https://www.devze.com 2023-02-20 16:08 出处:网络
I\'m having issues grasping how exactly to handle threads when using GeckoFX- it seems to throw errors constantly when trying to use the GeckoWebBrowser in other threads.

I'm having issues grasping how exactly to handle threads when using GeckoFX- it seems to throw errors constantly when trying to use the GeckoWebBrowser in other threads.

Specifically what I'm trying to do in this test application is generate a thumbnail of a webpage and display it on a form (not the WebBrowser control itself). When working singlethreaded, it works great:

private void browser_DocumentCompleted(object sender, EventArgs e)
{
    GeckoWebBrowser browser = sender as GeckoWebBrowser;
    if (browser.Url.ToString() != "about:blank")
    {
        webBrowserReady = true;
    }
}

private void btnGo_Click(object sender, EventArgs e)
{
    Xpcom.Initialize();

    GeckoWebBrowser browser = new GeckoWebBrowser();
    browser.Width = 1600;
    browser.Height = 1200;
    browser.DocumentCompleted += new EventHandler(browser_DocumentCompleted);
    webBrowserReady = false;        
    browser.CreateControl();
    browser.Navigate(txtUrl.Text);

    while (!webBrowserReady)
        Application.DoEvents();

    if (webBrowserReady)
        DrawWebBrowserToImage(browser);

    browser.Dispose();
}

However, if I move the browser creation and rendering into its own thread, I get a COM error when I try to call CreateControl():

Xpcom.Initialize();
Thread t = new Thread(new ThreadStart(()=>{
    GeckoWebBrowser browser = new GeckoWebBrowser();
    /* all the rest of the gecko stuff here too */
}
t.Start();

To fix this, I can move the Xp开发者_运维知识库com.Initialize call into the thread as well:

Thread t = new Thread(new ThreadStart(()=>{
    Xpcom.Initialize();
    GeckoWebBrowser browser = new GeckoWebBrowser();
    /* all the rest of the gecko stuff here too */
}
t.Start();

This works great... once. The second time I try to navigate, DocumentCompleted will only get fired once (with about:blank). After awhile a COM error will pop up as well. This seems to indicate to me that calling Xpcom.Initialize (a static method that can only be called once per application, and does nothing each subsequent time) permanently binds GeckoFX to that specific thread. Forever.

Is there a way to get Xpcom and GeckoFX to work properly in a multithreaded environment?


This will solve your prob, put it at the end of your nav request:

myGeckoFxBrowser.Navigate("about:blank");
myGeckoFxBrowser.Document.Cookie = "";
myGeckoFxBrowser.Stop();


GeckoFx can only be called from the same thread on which it was initialized (normally the UI thread)

so if you want to call GeckoFx Control, the method must come from the thread which initialized the GeckoFx Control,usually its UI thread as in your case. you can use BeginInvoke in Form class, try like this:

        this.BeginInvoke(new Action(() =>
        {
           browser.Navigate(txtUrl.Text);
            //your code
        }));
0

精彩评论

暂无评论...
验证码 换一张
取 消