Home c# Exclusion System.InvalidCastexception in the stream (Task)

Exclusion System.InvalidCastexception in the stream (Task)




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);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions