2011-10-12 13 views
5

im tratando de acceder a un cuadro de texto rico en otra forma im usando el siguiente código para hacerlo:operación hilo Cruz no es válida

Private Delegate Sub StringDelegateChat(text As String, window As ChatWindow) 
    Private Sub AppendTextChatWindows(text As String, window As ChatWindow) 
     Try    
      If window.RichTextBox1.InvokeRequired Then 
       window.Invoke(New StringDelegateChat(AddressOf AppendTextChatWindows), text, window) 
      Else 
       window.RichTextBox1.AppendText(text) 
       window.RichTextBox1.SelectionStart = window.RichTextBox1.Text.Length 
       window.RichTextBox1.ScrollToCaret() 
      End If 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString) 
     End Try 
    End Sub 

pero me da la operación de hilo cruzado error no válido, creo que hace esto porque se pierde la parte window.invoke de la instrucción if. También intenté reemplazar el If window.RichTextBox1.InvokeRequired Then por el If InvokeRequired Then pero queda atrapado en un bucle de continuación y se produce un error de desbordamiento de la pila.

Gracias Houlahan

+0

Probado ya window.InvokeRequired en lugar de window.RichTextBox1.InvokeRequired? –

+0

sí que se salta al otro y luego sigue la excepción:/ – Houlahan

+0

¿Está absolutamente seguro de que se ha creado la palanca de control? Incluso si lo es, no estaría de más comprobarlo dos veces ... – jmoreno

Respuesta

6

creo, en la línea 5, window.Invoke deben cambiar a window.RichTextBox1.Invoke.

Private Delegate Sub StringDelegateChat(text As String, window As ChatWindow) 
Private Sub AppendTextChatWindows(text As String, window As ChatWindow) 
    Try 
     If window.RichTextBox1.InvokeRequired Then 
      window.RichTextBox1.Invoke(New StringDelegateChat(AddressOf AppendTextChatWindows), text, window) 
     Else 
      window.RichTextBox1.AppendText(text) 
      window.RichTextBox1.SelectionStart = window.RichTextBox1.Text.Length 
      window.RichTextBox1.ScrollToCaret() 
     End If 
    Catch ex As Exception 
     MessageBox.Show(ex.ToString) 
    End Try 
End Sub 
0

No veo ningún error en su código. Es posible que desee comprobar si hay eventos que se activan al actualizar RichTextbox. Podrían estar causando un enhebrado cruzado.

Como solución a su problema, al trabajar con objetos, es menos probable que encuentre problemas de enhebrado cruzado.

3

Ha intentado:

Private Sub AppendTextChatWindows(text As String, window As ChatWindow) 
     Try    
      If window.RichTextBox1.InvokeRequired Then 
       window.RichTextBox1.BeginInvoke(New StringDelegateChat(AddressOf AppendTextChatWindows), text, window) 
       Exit Sub 
      Else 
       window.RichTextBox1.AppendText(text) 
       window.RichTextBox1.SelectionStart = window.RichTextBox1.Text.Length 
       window.RichTextBox1.ScrollToCaret() 
      End If 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString) 
     End Try 
    End Sub 

Básicamente, estoy preguntando por BeginInvoke en lugar de invocación. Aunque esperaría, como se menciona en otro cartel, que debe usar lo mismo para que marque el requerido para invocar. (es decir, tanto window.invokeRequirido & window.BeginInvoke o el control)

Cuestiones relacionadas