Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading ComboBox Text or SelectedValue causes a Cross-thread error?

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
So for simplicity I have several TextBoxes, a couple NumericUpDowns, and a ComboBox on a form. After adding text and selecting a value for the ComboBox the user presses start and processing is done in a separate thread using System.Threading.Thread. However, for some reason only when reading from the ComboBox do I get a Cross-thread operation not valid. It is easy enough to fix since all I'm doing is reading the text, but I don't understand why it has to be done for a ComboBox and as yet no other control. I'm not setting the value or I could understand it having an issue. I've used ComboBoxes in the past and threading, but I don't remember having this problem before now. Maybe I've just avoided the combination up till now. So the question is I don't understand why?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
VS2008

Code:
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim thrd As New System.Threading.Thread(AddressOf TestCB)
        thrd.Start()
    End Sub

    Private Sub TestCB()
        Dim tb As String
        Dim nud As Integer
        Dim cb As String

        tb = TextBox1.Text '<- Fine
        nud = NumericUpDown1.Value '<- Fine
        cb = ComboBox1.Text '<- Errors
    End Sub
End Class

Just seems really strange to me.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I thought I had mentioned it, but it looks like I didn't. SelectedText and SelectedValue cause the same problem.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top