Look at this code:

delegate void SetBrowserContentDelegate(WebBrowser b);

   if (browser.InvokeRequired)
                {
                    browser.Invoke(new SetBrowserContentDelegate(SetBrowserContent), browser);
                }
   else
   {
        browser.BringToFront();
        browser.DocumentText = "<html><body>example</body></html>";
   //this code here is executed and it gives an error saying this "Cross-thread operation..."
   }

        void SetBrowserContent(WebBrowser b)
        {
            b.BringToFront();
            b.DocumentText = message_;
        }

My question is why the SetBrowserContent method is not executed?The exception says that the form(i mean the form where the 'browser' is) is accessed by thread other than the thread it's created on.I'm fairly new to C# programming and this threads and delegates are very obscure to me,i'm doing something wrong but don't know what...Sorry for my bad english.Thanks in advance.

Recommended Answers

All 2 Replies

after form constructor add this line

CheckForIllegalCrossThreadCalls = false;

OMG i'm totally confused...the exception is thrown when i try to set DocumentText property of the WebBrowser.If i do something else like changing size,hiding it or something else it works fine.What's wrong with that DocumentText property?If i set the form CheckForIllegalCrossThreasCalls property to false,the application throws an exception but this time is the following "Controls created on one thread cannot be parented to a control on a different thread."When i remove "browser.DocumentText = "bla bla" it doesn't throw an exception!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.