none
CrossThreadMessagingException

    Question

  •  

     

     

     

    Tengo un Hilo corriendo y cuando desde ese hilo

    quiero accesar a una caja de texto para leer lo que tiene

    y luego escribir no me funcionaerfectamente

    de primera instancia no muestra el texto que debe mostrar

    y a veces se traba,

     

    cuando mando escribir a un log lo que contiene datos

    si lo hace perfectamente

     

    estoy utilizando delegados y el invoke

    que son formas seguras de accesar a controles 

    desde un hilo independiente y no funciona

     

    ademas tengo la instrucción:

      CheckForIllegalCrossThreadCalls = False
    

    y tampoco funciona

     

    me podrían ayudar pls

     

     

     

     

     

     

     

     

     

     

     

     

     

     

      Delegate Sub SetTextCallback(ByVal Datos As String)
      Delegate Function ReadTextCallback() As String
      Dim Hilo As Threading.Thread
    
    Private Sub cmdConectar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConectar.Click
        Skt.Start()
    
        Hilo = New Thread(AddressOf LeerSocket)
        Hilo.Start()
    
    
      
    End Sub
    
    
    
     Private Sub LeerSocket()
    
        Dim Datos As String
        Dim Anteriores As String
    
        Dim d1 As New ReadTextCallback(AddressOf ReadText)
        Dim d2 As New SetTextCallback(AddressOf SetText)
    
        Try
    
    
          While True
    
            While InQueue.TryDequeue(Datos)
              If Datos IsNot Nothing Then
    
    
                Anteriores = ""
                If txtRecibidos.InvokeRequired Then
                  ' Dim d1 As New ReadTextCallback(AddressOf ReadText)
                  Anteriores = d1.Invoke
                Else
                  Anteriores = ReadText()
                End If
    
                If txtRecibidos.InvokeRequired Then
                  'Dim d2 As New SetTextCallback(AddressOf SetText)
                  d2.Invoke(Anteriores & Datos)
                Else
                  SetText(Anteriores & Datos)
    
                End If
    
                Escribir_Archivo(Datos, "Recibidos")
                System.Threading.Thread.Sleep(10)
    
              End If
    
            End While
            System.Threading.Thread.Sleep(50)
          End While
    
    
        Catch ex As Exception
          MsgBox(ex.Message & ex.StackTrace, MsgBoxStyle.Exclamation, "")
        End Try
    
      
    End Sub
    
    
    
     
    Private Sub SetText(ByVal Texto As String)
        Me.txtRecibidos.Text = Texto
      
    End Sub
    
      
    Private Function ReadText() As String
    
        Return txtRecibidos.Text
    
      
    End Function
    
    
    
    

    An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred

     

    Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException

     

     

    Tuesday, August 24, 2010 12:17 PM

Answers

  • Change your "d1.Invoke" and "d2.INvoke" in :

    Me.Invoke(...).

    Basically when "InvokeRequired" is true, it means that invoke should be syncronized with the UI Thread. In this case to make this syncronization you should call <form,control>.Invoke(...)

    Hope it helps,


    Adriano
    • Proposed as answer by Jing0Moderator Monday, August 30, 2010 8:50 AM
    • Marked as answer by kakaroto2012 Monday, August 30, 2010 1:17 PM
    Tuesday, August 24, 2010 7:54 PM

All replies

  • Change your "d1.Invoke" and "d2.INvoke" in :

    Me.Invoke(...).

    Basically when "InvokeRequired" is true, it means that invoke should be syncronized with the UI Thread. In this case to make this syncronization you should call <form,control>.Invoke(...)

    Hope it helps,


    Adriano
    • Proposed as answer by Jing0Moderator Monday, August 30, 2010 8:50 AM
    • Marked as answer by kakaroto2012 Monday, August 30, 2010 1:17 PM
    Tuesday, August 24, 2010 7:54 PM
  • Hi OMAR OLGUIN,

     

    Sorry. This is English forum. If you have time, could you please translate the post to English?

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, August 30, 2010 9:07 AM
    Moderator