Calling GeckoFX multiple times inside a thread

0

I'm using GeckoFX v45 running in a STA thread. The code will works fine when I run it a once. I can navigate to a website and manipulate the document.

My problem is that I'm trying to run it in a loop which will run every 60 seconds.

When I call this._FFBrowser.Navigate( "https://duckduckgo.com" ); I get this error.

System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Gecko.nsIComponentManager'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{D604FFC3-1BA3-4F6C-B65F-1ED4199364C3}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

What am I doing wrong. Does Dispose() not release some components? I'm at a loss.

My Default.aspx page looks like this:

        for ( int i = 0; i < 10; i++ )
        {
            try
            {


                System.Threading.AutoResetEvent resultevent = new System.Threading.AutoResetEvent( false );

                bool visible = false; // Display the WebBrowser form

                FFBrowser browser = new FFBrowser( visible, resultevent );

                // Wait for the third thread getting result and setting result event
                while ( browser._IsDone == false )
                {
                    System.Threading.EventWaitHandle.WaitAll( new System.Threading.AutoResetEvent[] { resultevent } );
                }


                if ( visible )
                    browser.Dispose();


                System.Threading.Thread.Sleep( 5000 );
            }
            catch ( Exception ex )
            {
                throw ex;
            }
        }

My Gecko class looks like this:

public class FFBrowser : System.Windows.Forms.ApplicationContext
{

    public System.Threading.AutoResetEvent _ResultEvent;

    public int _NavigationCounter;
    public bool _IsDone = false;

    private Gecko.GeckoWebBrowser _FFBrowser;
    private System.Threading.Thread _Thread;
    private System.Windows.Forms.Form _Form;

    public FFBrowser( bool visible, System.Threading.AutoResetEvent resultEvent )
    {
        this._ResultEvent = resultEvent;

        this._Thread = new System.Threading.Thread( new System.Threading.ThreadStart(
            delegate
            {
                this._NavigationCounter = 0;

                Gecko.Xpcom.EnableProfileMonitoring = false;
                Gecko.Xpcom.Initialize( System.Web.Hosting.HostingEnvironment.MapPath( "~/Firefox" ) );


                this._FFBrowser = new Gecko.GeckoWebBrowser();

                this._FFBrowser.Navigating += this.FFBrowser_Navigating;
                this._FFBrowser.DocumentCompleted += this.FFBrowser_DocumentCompleted;

                if ( visible )
                {
                    this._Form = new System.Windows.Forms.Form();
                    this._Form.WindowState = System.Windows.Forms.FormWindowState.Normal;
                    this._Form.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
                    this._Form.Height = 850;
                    this._Form.Width = 1100;
                    this._Form.Top = 20;
                    this._Form.Left = 15;
                    this._FFBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
                    this._Form.Controls.Add( this._FFBrowser );
                    this._Form.Visible = true;
                }


                this._FFBrowser.Navigate( "https://duckduckgo.com" );


                System.Windows.Forms.Application.Run( this );


            } ) );

        // set thread to STA state before starting
        this._Thread.SetApartmentState( System.Threading.ApartmentState.STA );
        this._Thread.Start();
    }

    private void FFBrowser_Navigating( object sender, Gecko.Events.GeckoNavigatingEventArgs e )
    {
        // Navigation count increases by one
        this._NavigationCounter++;

    }
    private void FFBrowser_DocumentCompleted( object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e )
    {
        Gecko.GeckoDocument doc = this._FFBrowser.Document;

        System.Windows.Forms.Application.DoEvents();
        System.Threading.Thread.Sleep( 500 );

        this._IsDone = true;

        this._ResultEvent.Set();


    }



    protected override void Dispose( bool disposing )
    {
        if ( this._Thread != null )
        {
            this._Thread.Abort();
            this._Thread = null;
            return;
        }


        System.Runtime.InteropServices.Marshal.Release( _FFBrowser.Handle );
        this._FFBrowser.Dispose();

        if ( this._Form != null )
            this._Form.Dispose();

        base.Dispose( disposing );
    }


}

Here is a link to the full project source. https://github.com/ProgMaster1384/GeckfxTest

asp.net
geckofx
asked on Stack Overflow Jan 21, 2019 by Kathy Judd

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0