Hello everyone.
I have a problem when I use the WebBrowser object for navigating to multiple urls, one after the other. I have a list of urls. And for each url I want to navigate to this url, when DocumentCompleted event occurs, parse the information, and then go on to the next url in my list.
However, I need to lock the WebBrowser object in my foreach loop until a DocumentComplete event takes place, and then move on so it does not interrupt the download before it's completed. How can I do this?
Sort of like this:
foreach (string url in myUrlList)
{
if (!this.Browser.IsBusy)
{
this.Browser.Navigate(artNo);
this.Browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
}
}
..but this example doesn't work. Just to give an idea of my problem.
Thanks in advance for any suggestions!
Loading
Joakim AstromPosted Feb 8, 2010, 5:26 AM
I have tried using a Semaphore now which I guess does kind of what you suggest!? But it still "hangs" on the first call and never reaches the DocumentComplete event so I never get at chance to release it. Maybe you can help me with the actual code? I have tried initializing the Semaphore in different ways without success. Thanks for suggestions.
I initialize a global Semaphore:
Constructor:
pool = new Semaphore(1, 1);
MethodForNavigatingAllUrls:
try
{
foreach(string url in UrlList)
{
pool.WaitOne();
browser.Navigate(url);
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n" + e.StackTrace);
}
MethodDocumentComplete:
{
...yada yada
pool.Release();
}
Sam HobbsPosted Feb 4, 2010, 12:49 AM
Note that you should provide a timeout for the wait so that the code can deal with problems occuring during navigation.