DarkKight666
DarkKight666

Reputation: 351

InvalidCastException when not casting

I'm using the winforms Webbrowser control to load a webpage, and need to wait for the page to load.
So I have a while loop that checks the ReadyState property, but it keeps throwing an InvalidCastException on the line with the comparison.

I don't understand why I would be seeing this error as I'm not doing any casting, and casting isn't even necessary because the variable is of the same type as the enum value.

Could someone please shed some light on this?

Code Below:

while (wb.ReadyState != WebBrowserReadyState.Complete)
{
    Thread.Sleep(2500);
}

Edit:

Screenshot of exception window

Upvotes: 1

Views: 224

Answers (2)

DarkKight666
DarkKight666

Reputation: 351

Found the problem, was calling the readystate property from a different thread. Would have been nice to be told this was the problem as I was with other things, instead of a completely unrelated error message, but oh well.

Upvotes: 1

DGibbs
DGibbs

Reputation: 14618

Why don't you just subscribe to the DocumentCompleted event instead of using a loop?

private void wb_DocumentCompleted(object sender, EventArgs e)
{
    //do stuff after the page has loaded
}

Upvotes: 4

Related Questions