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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross-thread operation not valid 1

Status
Not open for further replies.

mancroft

Programmer
Joined
Oct 26, 2002
Messages
267
Location
GB
Cross-thread operation not valid

Hello

I have a program with the main thread (SampleForm) and another thread (thesecondthread).

Code:
Sub thesecondthread() 

Dim statuslabel2 As New Label
       
statuslabel2.Location = New System.Drawing.Point(10, 569)
:::
ETCETERA
:::
        
Controls.Add(statuslabel2)

End sub

When I try to create a label on thesecondthread, I get an error on the "Controls.Add(statuslabel2)" line:

Code:
Cross-thread operation not valid: Control 'SampleForm' accessed from a thread other than the thread it was created 
on.

Help!

Thank you.
 
Trying to access stuff on the main UI thread is not a good thing to do. You should only use another thread if you have some long running processing going on.
 
That looks like the new behaviour in vb.net 2005. You can;t access the form from another thread directly. You need to delegate.

Code:
 'start another thread to show how to add controls from another thread
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
    System.EventArgs) Handles MyBase.Load
        Dim t1 As New System.Threading.Thread(AddressOf thread1)
        t1.Start()
    End Sub

    Private Sub thread1()
        'Define a textbox to add
        Dim textbox1 As New TextBox
        textbox1.Location = New Point(50, 50)
        textbox1.Multiline = True
        textbox1.Width = 300
        textbox1.Height = 250
        textbox1.Text = "blah"
        'call a special sub to add the control as 
        'we are not allowed to add controls directly
        AddControlToForm(textbox1)
    End Sub

    ' prototype for the delegate 
    Private Delegate Sub AddControlToFormDelegate(ByVal ctrl As Control)

    Private Sub AddControlToForm(ByVal ctrl As Control)
        'Add string control ctrl to form me

        'InvokeRequired lets us know if we are in the wrong thread to
        'access form Me
        If Me.InvokeRequired Then
            ' make a delegate that will fire this sub again
            Dim delegate1 As New AddControlToFormDelegate(AddressOf _
        AddControlToForm)
            ' hold the parameters - the control being sent
            Dim parameters(0) As Object
            parameters(0) = ctrl
            'Ask form me to call this sub using the delegate with the parameters.
            Me.Invoke(delegate1, parameters)
            'it will call from the correct thread
        Else
            'InvokeRequired lets us know we are in the correct thread
            'so we can use listview1.add safely
            Me.Controls.Add(ctrl)
        End If
    End Sub
 
(I converted some code so ignore the comment about listview1.add. It should read "so we can use Me.Controls.Add safely")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top