Results 1 to 3 of 3

Thread: Another thread problem... Cross-thread operation not valid (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    294

    Another thread problem... Cross-thread operation not valid (Resolved)

    Does anyone know why I get this error?
    Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.

    It happens when I try to modify TextBox1's properties from a thread I created.

    The thread is a private variable in a class which raises an event. The handler of the event raised is where I tried to modify TextBox1.

    Code used to create thread
    sckTick = New Thread(AddressOf CallbackTick)
    sckTick.Start()

    Code of event handled, in thread sckTick
    Private Sub sckMainConnected() Handles sckMain.Connected
    MsgBox("Connected", MsgBoxStyle.Information, "")
    TextBox1.Enabled = True
    End Sub

    Any ideas what I'm doing wrong?
    Last edited by Cade; Sep 27th, 2005 at 06:29 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Another thread problem... Cross-thread operation not valid

    Have a look at the help topic for the Control.Invoke method for details on how to call methods of controls across thread boundaries.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    294

    Re: Another thread problem... Cross-thread operation not valid

    Delegate Sub FormControlsSub()
    Protected Sub EnableTxtBox()
    TextBox1.Enabled = True
    End Sub

    Private Sub sckMainConnected() Handles sckMain.Connected
    MsgBox("Connected", MsgBoxStyle.Information, "")

    Dim MSD As FormControlsSub
    MSD = AddressOf EnableTxtBox
    MSD.Invoke()
    End Sub

    Still get the same problem, this time, in EnableTextBox

    Edit: Got it
    Delegate Sub FormControlsSub()

    Protected Sub EnableTxtBox()
    TextBox1.Enabled = True
    End Sub

    Private Sub sckMainConnected() Handles sckMain.Connected
    MsgBox("Connected", MsgBoxStyle.Information, "")

    Dim MSD As FormControlsSub
    MSD = AddressOf EnableTxtBox
    Me.Invoke(MSD)
    End Sub
    Last edited by Cade; Sep 27th, 2005 at 06:28 AM.

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