Results 1 to 3 of 3

Thread: UI Thread and Secondary thread interaction

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    UI Thread and Secondary thread interaction

    With threading, the WebBrowser control behaves abnormally.
    While other controls allow accessing properties and error out when trying to set it, the WebBrowser control behaves just the opposite. It allows to set the property and errors out when trying to access it.

    Anyone knows the reason why?


    Here's a small test:
    Code:
       Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim th As New Threading.Thread(AddressOf DoWork3)
            th.IsBackground = True
            th.Start()
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim th As New Threading.Thread(AddressOf DoWork4)
            th.IsBackground = True
            th.Start()
        End Sub
    
        Private Sub DoWork3()
            TextBox1.Text = "test"                          '<-- this errors out
            WebBrowser1.DocumentText = "<html>test</html>"  '<-- this works
        End Sub
    
    
        Private Sub DoWork4()
            Dim txt = TextBox1.Text                         '<-- this works
            Dim wbdoc = WebBrowser1.DocumentText            '<-- this errors out
        End Sub
    Last edited by Pradeep1210; Jul 20th, 2010 at 02:44 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: UI Thread and Secondary thread interaction

    The simple fact is that you shouldn't be accessing any control properties at all on any thread other than the UI thread. Don't do it and it can never go wrong.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: UI Thread and Secondary thread interaction

    Sometimes you can access controls outside of the controls they were created on and sometimes you can't. It's a little odd that way, but this is nothing more than a coincidence. It may work in this instance, but fail in another. It has nothing to do with the specific control.
    Last edited by weirddemon; Jul 20th, 2010 at 07:39 PM.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width