Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Cross-thread operation not valid

Cross-thread operation not valid

Cross-thread operation not valid

Cross-thread operation not valid


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


Sub thesecondthread()

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

End sub

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


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


Thank you.

RE: Cross-thread operation not valid

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.

RE: Cross-thread operation not valid

That looks like the new behaviour in vb.net 2005. You can;t access the form from another thread directly. You need to delegate.


 '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)
    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
    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 _
            ' 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
            'InvokeRequired lets us know we are in the correct thread
            'so we can use listview1.add safely
        End If
    End Sub

RE: Cross-thread operation not valid

(I converted some code so ignore the comment about listview1.add. It should read "so we can use Me.Controls.Add safely")

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close