0

I have a loop and with in the loop i call

   webbrowser.navigate("www.example.com");

After this you know the documentcompleted function is called and i do some work int that function ,but after all the work is done ,Nothing happens the code does not come back to the loop and flow as it does in loops.Am i doing something wrong.

Thank you

ConfusedCoder
  • 175
  • 1
  • 6
  • 17

2 Answers2

2

This problem is caused by having a loop. The web browser's navigation can only complete and the DocumentCompleted event can only fire when your UI thread goes idle again. This is core to the way multi-threaded components work, they can only fire their 'the job is done' event on the UI thread when the UI thread is idle and pumps the message loop. The BackgroundWorker class for example has the exact same requirement.

The proper way to solve it is to create a state machine. You kick it off by telling the browser to navigate to the first url. You let your DocumentCompleted event handler process the response and then tell the browser navigate to the next url. Or stop when there are no more urls to process.

The improper way is to call Application.DoEvents() in the loop. That pumps the message loop and allows the events to fire. But is a very dangerous thing to do, DoEvents() is not selective about exactly what events are processed. Be sure to read this answer if you contemplate using it anyway.

The next way is the upcoming support for the async and await keywords in the next version of C#. Currently available in CTP.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

When you leave the webpage your javascript is on by navigating away, it's analogous to closing your application and starting some other application.

If you had a desktop application in which you wrote:

Application.ExitAndStartOtherApplication("SomeOtherApplication.exe");

You wouldn't expect whatever code came after that line to run.

It's the same thing.

Steve
  • 31,144
  • 19
  • 99
  • 122
  • hmm So any help or idea how to do it then? Just point me to the right direction ill figure out the rest. – ConfusedCoder Jul 03 '11 at 14:53
  • If you're in control of the other website you could navigate _back_ to the origional page when the javascript in "the documentcompleted function" is done. Use a cookie or pass some string around in the querystrings to maintain your state. Sounds like something **crazy** though. – Steve Jul 03 '11 at 14:56
  • im using desktop application, also i was thinking something like ,when we call another process in the code ,wait and when the process quits start from the next line something like that,, – ConfusedCoder Jul 03 '11 at 14:59
  • http://notions.okuda.ca/2009/06/11/calling-javascript-in-a-webbrowser-control-from-c/ Check out the section called "Calling C# from JavaScript" – Steve Jul 03 '11 at 15:05
  • You don't get me Steve, i am thinking when we reach the line to navigate, A new thread opens run the code and then exits and, then i move forward a multithread environment can this be implemented here .Does it make any sense? – ConfusedCoder Jul 03 '11 at 15:26
  • Yeah, it does, but the question is `run the code and then exits` assumes you know `when the code finished`. You don't. And the WebBrowser control doesn't have any magical method for telling you when all javscript code has finished running on the page. You'll have to invent that callback yourself - using the link I supplied. – Steve Jul 03 '11 at 15:34