Exception : (RPC_E_CANTCALLOUT_ININPUTSYNCCALL) when using WinForms application

0

I have created a WinForms application which uses BackgroundWorker. The methods in BackgroundWorer displays messages on a RichTextBox. The _TextChanged event handler scrolls to the end of the RichTextBox.

Now the problem is that, I get an

UnHandled COMException : 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)

when RichTextBox.ScrollToCaret() is called from the _TextChanged event handler. What could be the problem? How to solve this issue?

c#
winforms
comexception
asked on Stack Overflow Apr 17, 2015 by CDP21 • edited Apr 17, 2015 by Soner Gönül

1 Answer

0

As you've mentioned in the comments, you're accessing RichTextBox directly from the backgroundworker.

Accessing UI controls from multiple threads is strictly forbidden. Only the UI thread should access the controls.

You fix this you need to marshal the execution of the code that manipulates the UI controls (the RichTextBox in this case) onto the main UI thread, the thread that owns the RichTextBox.

You can do this in your backgroundworker code like this:

Invoke(new Action(() =>
{
    // code that manipulates the RichTextBox here
}));
answered on Stack Overflow Apr 17, 2015 by Lasse V. Karlsen

User contributions licensed under CC BY-SA 3.0