Why is sometimes WebBrowser.Dispose() launching Internet Explorer?
When I do the following (Actually I repeat the same whole code with different web pages):
Navig开发者_开发问答ator 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();
nav.Dispose();
Navigator is a Form that contains a public WebBrowser and that's it. You can test it yourself and I think the same will happen, but it doesn't happen every single time. You might need to put it in a loop and repeat it a few times for the problem to happen.
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();
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.
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();
精彩评论