1

I use the webbrowser control of .NET to open piles of urls,and the loop is called in the DocumentCompleted event.

Now I want to control the timeout.So I use a timer,and when timeout it will stop the webbrowser using the stop() function.

The question is: it seems that the stop function fires the DocumentCompleted event sometimes.So if the timer calls the next loop after stop the webbrowser,error happens.And if it doesn't call the next loop,sometimes the loop will be stopped in the middle.

The procedure like this(codes not related are deleted):

private string[] urls;//urls are stored here
private int index = 0;//next url index
private void loopFunc()
{
    timer.Enabled = true;
    wb.navigate(urls[index]);
    index++;
    return;
}

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    loopFunc();
}

private void timer1_Tick(object sender, EventArgs e)
{
    wb.stop();
    //loopFunc() or not?
}

I don't know for sure whether it fires the event or not,and I found nothing via google.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jacob
  • 619
  • 1
  • 5
  • 17
  • As i remember DocumentCompleted has a really strange behaviour. This helped me solved my issues http://stackoverflow.com/questions/2328835/why-is-webbrowser-documentcompleted-firing-twice . – Matija Grcic Mar 19 '12 at 09:06
  • @plurby:Multi-thread is my suggestion.But,also some strange behaviors. – Jacob Mar 19 '12 at 09:08
  • FYI, the WebBrowser control has nothing to do with C#. It is part of .NET, not part of C#. – John Saunders Mar 20 '12 at 11:42

1 Answers1

2

If I have understood your situation clearly this could solve your problem:

Timer On --->  loopFunc() --> goto url --> oncomplete -> start timer again -->
                  |
                  |---> Stop timer so it doesn't call loopFunc again

So stop the timer after loopFunc, when the download completes, loopFunc is called again:

private void loopFunc()
{
    timer.Enabled = true;
    wb.navigate(urls[index]);
    index++;
    timer.stop(); //<<<<<
    return;
}

And your tick should be:

private void timer1_Tick(object sender, EventArgs e)
{
  loopFunc();
}

Then start your timer again on document complete:

private void wb_DocumentCompleted(object sender, 
                  WebBrowserDocumentCompletedEventArgs e)
{
 wb.Stop();
 timer.Start();
}
gideon
  • 19,329
  • 11
  • 72
  • 113