Reputation: 3175
I have one method. You can even test it:
private readonly
public HtmlDocument Browse()
{
var _browser = new WebBrowser();
var link = "http://hotline.ua/sr/?q=allo";
var loadFinished = false;
_browser.DocumentCompleted += delegate { loadFinished = true; };
try
{
_browser.Navigate(link);
}
catch (Exception xx)
{
if (!(xx is UriFormatException))
throw;
loadFinished = false;
}
while ( ! loadFinished )
Thread.Sleep(50);
loadFinished = false;
return _browser.Document;
}
Then somewhere in code I call this method:
var doc = Browse();
Instead of getting a HtmlDocument I get to the infinite loop in :
while ( ! loadFinished )
Thread.Sleep(50);
It seems that DocumentCompleted is never fired. But in Web browser I can easily to get this page. Anybody knows why ? And what should I do to get a HtmlDocument ?
Upvotes: 1
Views: 865
Reputation: 3175
Oh sorry. I found a solution, after I post this topic:
That the reason why this event not fired:
I should not use
Thread.Sleep(50);
Because the event fired earlier and freed resourses. So thread get killed before it hits the document completed method.
If I change for:
Application.DoEvents();
It becomes work perfectly.
Thanks for you replies also !
Upvotes: 1
Reputation: 2005
You probably have exception which is not UriFormatException
so the
loadFinished is always false. Then DocumentComplete
cannot be reached.
Put a break point in Catch
in this line: loadFinished = false;
and check what exception is thrown.
Upvotes: 0
Reputation: 12212
I don't think that your delegate is correctly placed. You are not even taking the parameteres that this delegate needs. Take a look at:
How to use WebBrowser control DocumentCompleted event in C#?
Upvotes: 0