异常:(RPC_E_CANTCALLOUT_ININPUTSYNCCALL) 使用 WinForms 应用程序时
Exception : (RPC_E_CANTCALLOUT_ININPUTSYNCCALL) when using WinForms application
我创建了一个使用 BackgroundWorker 的 WinForms 应用程序。 BackgroundWorer 中的方法在 RichTextBox 上显示消息。 _TextChanged 事件处理程序滚动到 RichTextBox 的末尾。
现在的问题是,我得到一个
UnHandled COMException : 0x8001010D
(RPC_E_CANTCALLOUT_ININPUTSYNCCALL)
当从 _TextChanged 事件处理程序调用 RichTextBox.ScrollToCaret() 时。
可能是什么问题呢?如何解决这个问题?
正如您在评论中提到的,您正在直接从后台工作人员访问 RichTextBox。
严禁从多个线程访问 UI 控件。只有 UI 线程才能访问控件。
要修复此问题,您需要将操纵 UI 控件(在本例中为 RichTextBox)的代码的执行编组到主 UI 线程,即拥有 RichTextBox 的线程。
您可以像这样在后台工作程序代码中执行此操作:
Invoke(new Action(() =>
{
// code that manipulates the RichTextBox here
}));
我创建了一个使用 BackgroundWorker 的 WinForms 应用程序。 BackgroundWorer 中的方法在 RichTextBox 上显示消息。 _TextChanged 事件处理程序滚动到 RichTextBox 的末尾。
现在的问题是,我得到一个
UnHandled COMException : 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)
当从 _TextChanged 事件处理程序调用 RichTextBox.ScrollToCaret() 时。 可能是什么问题呢?如何解决这个问题?
正如您在评论中提到的,您正在直接从后台工作人员访问 RichTextBox。
严禁从多个线程访问 UI 控件。只有 UI 线程才能访问控件。
要修复此问题,您需要将操纵 UI 控件(在本例中为 RichTextBox)的代码的执行编组到主 UI 线程,即拥有 RichTextBox 的线程。
您可以像这样在后台工作程序代码中执行此操作:
Invoke(new Action(() =>
{
// code that manipulates the RichTextBox here
}));