我嘗試使用下面的代碼這樣做訪問的另一種形式的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