Results 1 to 3 of 3

Thread: [RESOLVED] Cross-thread operation not valid

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Resolved [RESOLVED] Cross-thread operation not valid

    In response to a "Cross-thread operation not valid: Control 'RecordAddedTextBox' accessed from a thread other than the one it was created on." error I have added a routine obtained from Microsoft Help and that is giving me a compile error.

    This is my original code:

    Code:
        Private Sub foo(ByVal tbx As Object)
            'when this code was in the Enter event handler the cursor was
            'not visible until the sounds played.
            Dim tb As TextBox = DirectCast(tbx, TextBox)
    
            Select Case tbx.name
                Case "NameTextBox"
                    speaker.Speak("Name")
                Case "RecordAddedDateTextBox"
                    Me.RecordAddedDateTextBox.Text = Now()  <-- ERROR HERE
                Case "ConversationTextBox"
                    speaker.Speak("Conversation")
            End Select
            Threading.Thread.Sleep(250)
        End Sub
    And this is the code which I added:
    Code:
        Private Sub SetText(ByVal [text] As String)
    
            ' InvokeRequired required compares the thread ID of the
            ' calling thread to the thread ID of the creating thread.
            ' If these threads are different, it returns true.
            If Me.RecordAddedDateTextBox.InvokeRequired Then
                Dim d As New SetTextCallback(AddressOf SetText)
                Me.Invoke(d, New Object() {[text]})
            Else
                Me.RecordAddedDateTextBox.Text = [text]
            End If
        End Sub
    presumably to be called from my subroutine. But SetTextCallback comes up as undefined. Since this is a strange to me I don't know how to fix it. I was hoping someone could help me and save my hours of trial and error. Is there a better way?

    Of course there is more code involved which calls the subroutine with the error and I can post that if needed.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Cross-thread operation not valid

    You forgot to declare a delegate SetTextCallback

    vb Code:
    1. Private Delegate Sub SetTextCallback(ByVal text As String)
    2.  
    3.  Private Sub foo(ByVal tbx As Object)
    4.         'when this code was in the Enter event handler the cursor was
    5.         'not visible until the sounds played.
    6.         Dim tb As TextBox = DirectCast(tbx, TextBox)
    7.  
    8.         Select Case tbx.name
    9.             Case "NameTextBox"
    10.                 speaker.Speak("Name")
    11.             Case "RecordAddedDateTextBox"
    12.                 Me.SetText(Now.ToString())
    13.             Case "ConversationTextBox"
    14.                 speaker.Speak("Conversation")
    15.         End Select
    16.         Threading.Thread.Sleep(250)
    17.     End Sub
    18.  
    19. Private Sub SetText(ByVal [text] As String)
    20.  
    21.         ' InvokeRequired required compares the thread ID of the
    22.         ' calling thread to the thread ID of the creating thread.
    23.         ' If these threads are different, it returns true.
    24.         If Me.RecordAddedDateTextBox.InvokeRequired Then
    25.             Dim d As New SetTextCallback(AddressOf SetText)
    26.             Me.Invoke(d, New Object() {[text]})
    27.         Else
    28.             Me.RecordAddedDateTextBox.Text = [text]
    29.         End If
    30.     End Sub

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Cross-thread operation not valid

    Simple. Thanks.

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