2

Why I'm getting this error?

enter image description here

System.InvalidCastException was unhandled by user code
  Message=Specified cast is not valid.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
       at System.Windows.Forms.WebBrowser.get_Document()
       at System.Windows.Forms.WebBrowser.get_DocumentStream()
       at System.Windows.Forms.WebBrowser.get_DocumentText()
       at SiteBot.MainWindow.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:\Documents\Visual Studio 2010\Projects\SiteBot\MainWindow.cs:line 35
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
  InnerException: 
7
  • 12
    See that link that says "Copy exception detail to clipboard"? Please paste the result of that into your question. Commented Mar 11, 2011 at 2:19
  • I'd Enable Framework Source Stepping and Symbol Server. This will let the exception show in the code that is throwing it. Commented Mar 11, 2011 at 2:20
  • Uhhh... can you access a WebBrowser component on the non-GUI thread? Commented Mar 11, 2011 at 2:21
  • Would be good if you could paste the value for the webBrowserMain.DocumentText Commented Mar 11, 2011 at 2:24
  • 8
    Haahahaha... anyone go to the website? This dude is trying to milk money by automating the clicking on ads. Commented Mar 11, 2011 at 2:57

2 Answers 2

7

The following solves your cross thread issue.

public delegate string GetStringHandler();
public string GetDocumentText()
{
    if (InvokeRequired)
        return Invoke(new GetStringHandler(GetDocumentText)) as string;
    else
        return webBrowser.DocumentText;
}

if (regAddId.IsMatch(GetDocumentText()))
{
}
Sign up to request clarification or add additional context in comments.

Comments

0

I get a threading exception with this test:

public class Test
{
    private readonly WebBrowser wb;

    public Test()
    {
        wb = new WebBrowser();
        var bw = new BackgroundWorker();
        bw.DoWork += DoWork;
        bw.RunWorkerAsync();

        while (bw.IsBusy)
        {
            Thread.Sleep(10);
            Application.DoEvents();
        } 
    }

    private void DoWork(object sender, DoWorkEventArgs e)
    {
        wb.Navigate(@"www.clix-cents.com/pages/clickads");
        Thread.Sleep(1000);
        var regex = new Regex("onclick=\\'openad\\(\"([\\d\\w]+\"\\);");
        regex.IsMatch(wb.DocumentText);
    }
}

public class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
        new Test();
    }
}

The exception looks like this: Exception

Since WebBrowser is really just a wrapper around IE's ActiveX control, you'll need to be careful about threading issues. I think what you really want to use here is a WebClient and not a WebBrowser, but I'm just guessing about your application.

[EDIT]

Like @Fun states you can just Invoke over to the GUI thread (assuming thats where the control was created. I'd still recommend using a WebClient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.