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
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.
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.