Hello,
I have a VS2003 app that I moved to VS 2005. I am trying to correct
the error that I now receive when the app runs. In several places I am
getting a InvalidOperationException "Cross-thread operation not valid:
Control '' accessed from a thread other than the thread it was created
on." I have been reading alot about this and I understand why I am
getting the error, but I am having problems fixing the problem.
Basically I have a custom control that inherits from a RichTextBox. In
vs 2003 something like this code:
this.SelectionStart = int.MaxValue;
Translates to this in vs2005:
delegate void SetSelectionStartCallback(int value);
private void SetSelectionStart(int value)
{
if (this.InvokeRequired)
{
SetSelectionStartCallback d = new
SetSelectionStartCallback(SetSelectionStart);
this.Invoke(d, new object[] { value });
}
else
{
this.SelectionStart = value;
}
}
this.SetSelectionStart(int.MaxValue);
This part I am OK with, but for simple "get" calls to the base
RichTextBox's properties I do not know how to code these. For example,
this next line of code throws the same error. How do I go about
retrieving the Text property in a thread safe manner? Is it possible
to retreive the instance to the control itself "this" in a thread safe
manner?
if (this.Text.Length > 0)
I do not want to take the easy way out and set
CheckForIllegalCrossThreadCalls=false.
Thanks!
-J