Good day.
There is such a design required to wait for the full page load in the WebBrowser component:
while (WebBrowser1.ReadyState! = WebBrowserReadyState.complete)
{
Application.doevents ();
}
In the main thread, it works fine, but when executing in Task using ASYNC AWAIT issues an exception to System.InvalidCasteException to this line:
while (WebBrowser1.ReadyState! = WebBrowserReadyState.complete)
please tell me why this happens and how is it possible to get around?
Thank you.
Answer 1, Authority 100%
Judging by what the Internet suggests, the problem is in the ReadyState property itself. It is not thread safe, so it is recommended to use Invoke.
Example, quite worker.
Private Async Void Button1_Click (Object Sender, Eventargs E)
{
WebBrowser1.navigate ("http://www.microsoft.com");
await wait ();
}
Private Task Wait ()
{
Return Task.run (async () = & gt;
{
While ((WebBrowSerReadyState) WebBrowser1.invoke (New Func & Lt; WebBrowserReadyState & GT; (() = & gt; WebBrowser1.ReadyState))! = WebBrowserReadyState.comPlete)
AWAIT TASK.DELAY (100);
});
}