代码之家  ›  专栏  ›  技术社区  ›  Kathy Judd

在线程中多次调用geckofx

  •  0
  • Kathy Judd  · 技术社区  · 7 年前

    我使用的是运行在sta线程中的geckofx v45。我只运行一次代码就可以正常工作了。我可以导航到网站并操作文档。

    我的问题是我试着在一个每60秒运行一次的循环中运行它。

    当我打电话 this._FFBrowser.Navigate( "https://duckduckgo.com" ); 我知道这个错误。

    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)).'
    

    我做错什么了。dispose()不释放某些组件吗?我不知所措。

    我的default.aspx页面如下所示:

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

    我的壁虎课是这样的:

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

    以下是指向完整项目源的链接。 https://github.com/ProgMaster1384/GeckfxTest

    0 回复  |  直到 7 年前
    推荐文章