I recently started a thread on this same topic and marked it as resolved but as I moved to the next stage I've decided it isn't. I'm not sure of the protocol and I don't see a way to remove the "Resolved" tag so I'm starting another. If this is not the proper etiquette please let me know.

Not being able to wait for the WebBrowser to load is really difficult for me to wrap my head around logically. If I have to navigate to a single page and get some text for instance I can understand how to use a sub associated with the DocumentCompleted event. And if I have to iterate thru a bunch of pages I can see how to use two subs with .navigate at the end of each to effectively create a loop. Though I don't think it elegant it works. But now in the same form I have other things I need to do with the WebBrowser control and there's only one sub associated with DocumentCompleted. Additionally the way this new routine works I'm having problems trying to figure out how to structure it. My understanding is probably incomplete but what I'm coming up with is that each part of the process needs to be broken into subs and each sub needs to end with a Navigate except the very last one. Perhaps someone can explain to me the gap in my understanding.

Let me give you a practical example. I have a WinForm with a single WebBrowser and a couple of buttons. After load the user logs into the site and hits a button and the WebBrowser loops thru a bunch of pages and extracts some data. The second function a user specifies and runs a report. Once loaded, which can take some time, they click a button and the program extracts a bunch of call IDs from a table then plows thru every call record by navigating to that page. Once done collecting all that it does a few other things and prompts the user to save a file.

According to the previous advice I don't see how I can do this all with one DocumentCompleted sub. I was thinking of a condition based on the Sender parameter but the sender will always be the WebBrowser, not the sub that called it. I suppose I could create a shared variable for condition and manually set that each time and ad a select case to the document completed. But then I'll have to put all the rest of my code in the DocumentCompleted sub? I must be missing something because this all seems incredibly complicated and confusing. I really hope there's something simple I'm missing.

If it turns out there isn't an elegant solution would it slightly less evil to just create a loop with a 100mS thread sleep to wait for the document to be done? I've seen examples from Ident for instance which used DoEvents but according to the responses that's a bad practice which makes sense to me. If this is an OK idea I was hoping someone might help me select the best Do While condition. I believe there are some scripts running on this page and I suspect that properties like DocumentCompleted might not be sufficient. IE it seems it takes a little while longer for the scripts to do their thing. I was thinking of using IsBusy and having a couple counters. One for max iterations before it gives up and another to count a few iterations of the state to make sure it's really and finally complete. Like 3 100mS iterations. Though it might not be necessary here.

As an aside I tried to go back to using WebClient but I haven't been able to figure out how to get the authentication to work. I might go back to that given it's probably an easier problem to solve.

Sorry for the lengthy post and thank you for taking the time to consider it.