Much to my discontent, I need to use a WebBrowser control in one of my apps.
One of the things I also need to do is wait for an element to become visible/class changes/etc, which happens well after the DocumentCompleted
event is fired, making the event close to useless in my case.
So currently I have something like...
while (webBrowser.Document?.GetElementById("id")?.GetAttribute("classname") != "class")
{
Application.DoEvents();
Thread.Sleep(1);
}
Now I've read in multiple places that DoEvents()
is evil and can cause lots of issues, so I thought about replacing it with Task.Delay()
as such:
while (webBrowser.Document?.GetElementById("id")?.GetAttribute("classname") != "class")
{
await Task.Delay(10);
}
So my question is, other than the obvious facts that the Thread.Sleep()
will block events for 1ms and that the Task.Delay()
has a bigger delay set in the example above, what are the actual differences between doing the two approaches, which is better and why?
PS: Please stick to the question, while I wouldn't necessarily mind other ideas on how to fix the WebBrowser
control issue itself by using something else (js injection comes to mind), this is not the place to answer that, this question is about how these two bits of code differ and which would be considered better.