我尝试使用下面的代码这样做访问的另一种形式的IM丰富的文本框:跨线程操作无效
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
,但我得到了跨线程操作无效的错误,我认为它这样做因为它漏掉了if语句的window.invoke
部分。我也尝试将If window.RichTextBox1.InvokeRequired Then
替换为If InvokeRequired Then
,但它会被连续循环捕获,并引发堆栈溢出错误。
感谢 Houlahan
已尝试window.InvokeRequired而不是window.RichTextBox1.InvokeRequired? –
是的,只是跳到其他,然后抛出异常:/ – Houlahan
你确定控件句柄已经创建?即使你是,也许不会受到双重检查...... – jmoreno