Why is sometimes WebBrowser.Dispose() launching Internet Explorer?

后端 未结 3 617
春和景丽
春和景丽 2021-01-07 01:59

When I do the following (Actually I repeat the same whole code with different web pages):

        Navigator nav;
        nav = new Navigator();
        nav.B         


        
相关标签:
3条回答
  • 2021-01-07 02:14

    Make your Navigator form look like this:

    public partial class Navigator : Form {
        private bool done;
    
        public Navigator() {
            InitializeComponent();
            Browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Browser_DocumentCompleted);
        }
        void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
            if (e.Url == Browser.Url) done = true;
        }
        public bool Completed {
            get { return done; }
        }
    }
    

    And your loop like this:

            Navigator nav;
            nav = new Navigator();
            nav.Browser.Navigate("http://www.amazon.com/gp/search/ref=sr_adv_b/?search-alias=stripbooks&unfiltered=1&field-keywords=&field-author=&field-title=&field-isbn=0471848573+&field-publisher=&node=&field-p_n_condition-type=&field-feature_browse-bin=&field-binding_browse-bin=&field-subject=&field-language=&field-dateop=&field-datemod=&field-dateyear=&sort=relevanceexprank&Adv-Srch-Books-Submit.x=18&Adv-Srch-Books-Submit.y=4"); //        http://msn.com"); //stackoverflow.com/questions/3661536/while-true-application-doevents-doesnt-know-when-the-program-finishes");
            this.Enabled = false;
            while (!nav.Completed) Application.DoEvents();
            this.Enabled = true;
            nav.Dispose();
    
    0 讨论(0)
  • 2021-01-07 02:31

    I had the same problem when I tried to invoke Dispose() function of a WebBrowser in a ThreadPool Queued executing.

    I started a Timer in the delegated of Control.Invoke to resolve it. This is my code:

        public delegate void SucceedDelegate();
        // calling by ThreadPool.QueueUserWorkItem(succeed);
        protected void succeed(Object obj){
            Application.OpenForms[0].Invoke(new SucceedDelegate(succeed0));
        }
        private void succeed0()
        {
            Timer timer = new Timer();
            timer.Interval = 1;
            timer.Tick += succeedHandler;
            timer.Start();
        }
        private void succeedHandler(object sender, EventArgs e)
        {
            ((Timer)sender).Stop();
            succeed00();
        }
        private void succeed00()
        {
            //call WebBrowser.Dispose() here
        }
    

    It's ugly, but working well.

    0 讨论(0)
  • 2021-01-07 02:37

    Try to add another DoEvents AFTER the loop, just before the nav.Dispose()

                Navigator nav;
                nav = new Navigator();
                nav.Browser.Navigate("http://www.amazon.com/gp/search/ref=sr_adv_b/?search-alias=stripbooks&unfiltered=1&field-keywords=&field-author=&field-title=&field-isbn=0471848573+&field-publisher=&node=&field-p_n_condition-type=&field-feature_browse-bin=&field-binding_browse-bin=&field-subject=&field-language=&field-dateop=&field-datemod=&field-dateyear=&sort=relevanceexprank&Adv-Srch-Books-Submit.x=18&Adv-Srch-Books-Submit.y=4");
                while (nav.Browser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
                Application.DoEvents(); // will avoid launching of IE
                nav.Dispose();
    
    0 讨论(0)
提交回复
热议问题