I have a win forms application that contains a Web Browser control. I need to be able to add delay between operations on the web-browser due to the asynchronous nature of navigating.
The Document_Complete event is worthless as it does not take into account that a page may contain multiple AJAX requests. The event ofter fires many times.
UPDATE
The AJAX Requests are made when the page is loaded. So the page loads and content in some DIV is fetched via an HTTP request. So the the Document_Complete event is raises when the document first loads and then when each (AJAX) HTTP request returns. No Bueno.
UPDATE2
My application attempts to read HtmlElements from the Webbrowser.Document object. Because the code executes faster than the HTTP Requests return… the document object does not contain all of the html elements.
What I need is some way to delay the call of methods in the main thread. I have tried using a timer:
private void startTimer()
{
timer.Interval = 2000;
timer.Start();
while (!BrowserIsReady)
{
//Wait for timer
}
}
This locks up the thread and the tick event never fires. This loops never ends.
I want to run a series of methods like this:
Navagate("http://someurl.com");
//delay
ClickALink();
//delay
Navagate("Http://somewhere.com");
//delay
Can I solve this problem with a timer and the BackgroundWorker? Can someone suggest a possible solution?
Thanks for all the suggestions. This is the “Delay” solution I came up with last night. It feels like using a steam roller to crack a peanut but I couldn’t find a more ‘elegant’ answer to this particular problem.
I am spinning off a new worker thread that invokes a method called “AddDelay”. This method will put the worker thread to sleep for some interval of time. My main (UI) thread loops on the Thread.IsAlive condition while allowing the application to receive OS messages if the thread has not completed.
This seems to do the trick.