I'm running a BackgroundWorker
which suppose to update my UserControl
. I tried Invoking after checking InvokeRequired
property:
private void bgwHighlightText_DoWork(object sender, DoWorkEventArgs e)
{
tmpRich.SelectedRtf = myRtf;
if (_ucResultRich.InvokeRequired && _ucResultRich.rchResult.InvokeRequired)
_ucResultRich.Invoke(new Action(() => _ucResultRich.rchResult.Rtf = tmpRich.Rtf)); // Debug pointer stops here
//Rest of the code
}
I also tried to invoke the RichTextBox
inside my UserControl
directly:
_ucResultRich.rchResult.Invoke(new Action(() => _ucResultRich.rchResult.Rtf = tmpRich.Rtf));
But when I debug the code, it just stops running the rest of the code with no error.
Both _ucResultRich.InvokeRequired
and _ucResultRich.rchResult.InvokeRequired
return true
.
Am I doing something wrong here?
Update
I put the Invoke
part in try catch
and now I can get the following error from the exception message:
Cross-thread operation not valid: Control '' accessed from a thread
other than the thread it was created on.
Is it because it cant determine the control? Cause it shows it like Control ''
.