Results 1 to 3 of 3

Thread: accessing a control or a property of an object that are created in a diffrent thread

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Resolved accessing a control or a property of an object that are created in a diffrent thread

    How is it possible to access a control (f. ex. changing the text property of a Label) or a property of any custom object, when the control or custom-object are created in a diffrent thread than the one from where I want to update them?

    Here is an example of what does not work :

    VB Code:
    1. Public Class Form1
    2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.         StartDoWeighings()
    4.     End Sub
    5.     Dim Thread As System.Threading.Thread = New System.Threading.Thread(AddressOf Me.ThreadSub)
    6.     Private Sub StartDoWeighings()
    7.         If Not Thread.IsAlive Then
    8.             Thread.Start()
    9.         End If
    10.     End Sub
    11.     Sub ThreadSub()
    12.         Label1.Text = Date.Now.ToLongTimeString
    13.     End Sub
    14. End Class

    And here is the error when the button is clicked :
    "Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on."


    Thank you very much for any help !

    Regards,
    Fabian
    Last edited by fabianus; Jul 26th, 2005 at 04:26 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width