473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1779

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
3
by: Development | last post by:
I am creating 10 threads that do ecatly the same thing. I am having a very hard time debugging this scenario. It seems that by default when you are doing f10 or f11 all threads execute. You can...
2
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
8
by: apatiuthai | last post by:
Can I use one Imageslist for all form in my apps? like data module in delphi thanks very much
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.