Question Getting Threads working the way i need them

fireup6

New member
Joined
Jan 14, 2011
Messages
2
Programming Experience
Beginner
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:
VB.NET:
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.
 
Last edited:
Your code doesn't really make much sense. What are you really trying to achieve? I imagine that you aren't starting a new thread just to display the text of a TextBox.
 
Your code doesn't really make much sense. What are you really trying to achieve? I imagine that you aren't starting a new thread just to display the text of a TextBox.

Well i have a sub in my module that needs to access (read/write) the form controls from time to time. I need this sub to run in a new thread when i hit the "Start" button. I encounter the same problems as i do with a simple MsgBox. There really isn't a big difference.
 
Again, that doesn't really make much sense. The point of starting a new thread is to do work in the background while the UI thread remains responsive, yet you say that the purpose of this thread is to read and write data in controls. The controls ARE the UI, so reading data from the and writing data to them is inherently a foreground operation. What you might do is read data from the controls and then pass it to a secondary thread for processing. That makes sense. Actually interacting with the UI directly on any thread other than the UI thread is not allowed by default and is something that you should never do. There are mechanisms available to interact with the UI indirectly, but it's hard to recommend a course of action when, again, you haven't actually explained what you're trying to achieve. You say that you want to access controls. Why? What is the actual purpose? If we know that then we can explain how to achieve it.
 
Back
Top