Using a WebBrowser Control In A Loop?

4

47computers

I'm trying to write a small application that will take a given array
of strings (which happen to be URLs, or IDs appended to the end of a
given URL) and will loop through the array with each iteration
navigating a WebBrowser control to that URL and printing it (the
default printer on the machine will be a PDF writer).

However, the problem I'm having is that the code doesn't seem to wait
for the WebBrowser control to load the document. It loops through
immediately and then just prints 4 copies of the last document
(currently, for testing, the array has only 4 elements).

Is there a way to get it to "wait" for the WebBrowser control?
Spawning off threads for this might not be a good idea because when
this goes to a scale testing environment it'll be fetching over 4
million URLs, which I imagine will spawn too many threads.

Or is there perhaps a better way to do what I'm trying to do? (That
is, print a hard copy (PDF) of each report object in our database, of
which there are over 4 million, preserving the various complicated
HTML that gets generated on the fly when viewing a report.)

Any help would be much appreciated, thank you.


Regards,
David
 
S

Sheng Jiang[MVP]

Handle the WebBrowser.DocumentCompleted Event and check the
WebBrowser.ReadyState Property.
For more information, see the article "How To Determine When a Page Is Done
Loading in WebBrowser Control" in Knowledge Base.
 
M

Marc Gravell

What does your loop/navigate/print code look like at the moment? I'm
guessing you'd need to catch the Navigated event (checking the url of
the new page), send a Print(), and then perhaps take a breather
(5seconds?) to let it catch up, perhaps polling IsBusy (no idea if
this gets set for printing; documentation only mentions loading).

However! Indeed, a browser script could be a pain. There might (if
lucky) be a console switch to print from a url to a printer?
Possibly...
Depending on the output, a few tools might be in order; how complex is
the HTML? Does it involve CSS? Javascript? In increasing complexity,
three HTML->PDF products that leap to mind are HTMLDOC, ABCPDF, and
ActivePDF; the first lacks CSS and JS; the second JS (IIRC); the 3rd
automates IE, but is a behemoth and I have never been impressed with
the performance & cost (especially in combination). There will be
other products too, I am sure.

Another way to save for prosperity might be "chm". This doesn't appear
(AFAIK) on the WebBrowser interface; shdocvw may expose it if you hook
at a lower level, or you might be able to code via
Document.ExecCommand ("SaveAs"?), or a DHTML invoke. Heck, SendKeys
might work (but be a bit rough around the edges).

Probably more questions in there than answers, but it might help
you...

Marc
 
H

Hakan Fatih YILDIRIM

you can check if the webbrowser is busy or not like this

if ( ! ( webBrowser1.IsBusy )){
DoWork();
}
Regards,
Hakan Fatih YILDIRIM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top