I have a simple form which starts a new thread, and this thread displays the value of a control (TextBox)
Form contains:
TextBox with default value of "Default"
StartButton which starts a new thread
Form:
Public Class Form
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'The reason i declare the thread like this is because it allows me to pass parameters to the DisplayBox() sub, or any other sub, which i need in my main application.
Dim t As New Thread(DirectCast(Sub() DisplayBox(), ThreadStart))
t.Start()
End Sub
Public Sub DisplayBox()
MsgBox(TextBox1.text)
End Sub
End Class
Simple enough. I start it, replace the "Default" text in my TextBox to anything I want, hit start, and my new thread MsgBox's me the new value.
Now i try something more useful to me. I put the DisplayBox() sub into it's own module. Then i run the program again, and this time i get this error from the MsgBox in the the module:
"An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."
Now i don't exactly understand the error. Especially the part about creating a form. But i did some googling and i found out how to set the new thread to running in a single-threaded apartment:
"t.SetApartmentState(ApartmentState.STA)"
Seems to work, the previous error disappears. But now when i start the thread, with DisplayBox() still being in it's own module, MsgBox always displays the same "Default" value from the TextBox in the Form. Whatever i change the value of Textbox1.Text to, and then run the thread, the MsgBox will always return it's initial value "Default".
Sorry If I'm not very clear, I'm up very late trying to figure this out. If anyone could explain to me why this is occurring and why i got that original error before, i would be very grateful. Thanks.