InvalidCastException, but just in thread

Hello,

I'm currently writing a program and stumbled across the following problem:

I want to poll the title of a frame of a website (and react if it changes). Therefore I created a thread. But everytime I try to access the the frame count or frame title I get this InvalidCastException. But outside the thread I can access it without any problems. I tried alot of things but nothing worked. Here is the relevant current code (with some of my tries):

public partial class Form1 : Form
{
bool tmp = true;

public Form1()
{
InitializeComponent();
webBrowser1.Url = new Uri("http://mysite");
webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
}

void webBrowser1_StatusTextChanged(object sender, EventArgs e)
{
if (webBrowser1.Document.Window.Frames.Count > 2 && tmp == true) //no exception here...
{
Thread t1 = new Thread(new ThreadStart(pollFrameTitle));
t1.Start();
tmp = false;
}
}

private void pollFrameTitle()
{
Thread.Sleep(500);
int cnt = webBrowser1.Document.Window.Frames.Count; //InvalidCastException (the method returns int)
if (cnt > 2)
listBox1.Items.Add(webBrowser1.Document.Window.Frames[2].Document.Title.Equals(Convert.ToString("Title "))); //writes in a list (for testing) if the title of the page is what I'm looking for. If I uncomment the int cnt =... above I get the InvalidCastException here
}
}

Does anyone know the reason why this is happening Thanks for any answer!


Answer this question

InvalidCastException, but just in thread

  • Alessandro Moacyr Duarte

    wait for the last document DocumentComplete event (not possible without accessing native interface, though).

    suggest reading

    Understanding Classic COM Interoperability With .NET Applications



  • gajar

    That's not the problem. I also tried to start the thread in the constructor. I want to run it in a thread because I want to poll the title the whole time (i.e. later there will be something like a while(true) in the thread). I have really no clue why I'm getting that exception. I tried the same with a normal method call (instead of the thread) and it worked. But that doesn't really help me..

  • Andrei G.

    Now I don't know why you get that exception but you could use a parameterized thread start and pass the framecount that way you don't need to re-retrieve the framecount.

  • enric vives

    Yes but your thread will not run until the webbrowser has finished loading the document. Not sure why you want to run the code in a seperate thread...

  • Sean Reilly

    Is the WebBrowser 2.0 object still a COM object My problem with the DocumentCompleted event is, that I want to handle a HTTP404 which occurs in a frame. And as far as I know (not sure though) I don't get the event in the case of a HTTP404. And I still don't really understand why I'm getting an InvalidCastException when I try to access the variable from within the thread but not when I access it in the method which starts the thread.

  • SquadShun

    what you need is to handle the DWebBrowserEvents2.OnTitleChange event. If you are using the .Net WebBrowser control, you may need to sink the native interfaces



  • Lbo

    The problem is that I want to poll the title of the frame in the thread and there I get the same exception as with the count.

  • InvalidCastException, but just in thread