471,347 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,347 software developers and data experts.

MultiThreaded apps and Getting values?

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

Feb 20 '06 #1
1 1718

<kn*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
| 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
|

One option is to use the MethodInvoker...

Invoke(new MethodInvoker(delegate() {
if (this.Text.Length > 0)
....
}),null);
Willy.
Feb 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.