Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

mancroft (Programmer) (OP)
12 Aug 05 10:57
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.
RiverGuy (Programmer)
12 Aug 05 12:51
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.
Helpful Member!  jo0ls (Programmer)
12 Aug 05 13:46
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
jo0ls (Programmer)
12 Aug 05 13:49
(I converted some code so ignore the comment about listview1.add. It should read "so we can use Me.Controls.Add safely")

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!

Back To Forum

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