By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Cross-thread operation not valid ???

P: n/a
I receive this message:
"System.InvalidOperationException: Cross-thread operation not valid: Control
[your_control_name_here] accessed from a thread other than the thread it was
created on"
....
this is a exception. Can fix it?

Nov 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Le Minh wrote:
I receive this message:
"System.InvalidOperationException: Cross-thread operation not valid:
Control [your_control_name_here] accessed from a thread other than
the thread it was created on"
...
this is a exception. Can fix it?
Yes, you can fix it. The exception means that your program accessed a GUI
element from a thread other than the main thread, and that's a no-no.

Use Control.Invoke (or BeginInvoke) to execute the call in the correct
thread.

-cd
Nov 23 '06 #2

P: n/a

Hi,
In VS 2005 Desinging Microsoft Take care abot the major issue. Thread
Synchnization is also change a lot from the 2003 to the 2005 VS.
Actuall when the programm executed, system assign the one thread for
the entire GUI creation and take the changes on the GUI, that will be
[STA Thread]. That thread has the only permission to take the changes
and control the other GUI elements creataed by the STA thraed. If u
create a another threads with the use of System.Threadding.thread,
these thread are not have a enoff previliges to change the GUI elements
on the forms. So u need to write the delegaes from the main thread and
call the delegate function from u r chaild threads, By using this
method u can solve this problem .

public delegate void AddnewTextDelegate(string str);
public void AddTextToTextBox(string str)
{
if (this.textBox1.InvokeRequired)
{
this.Invoke(new AddnewText(AddTextToTextBox), str);
}
else
{
this.textBox1.Text += str;
}

}

The Above exampele shows the add the test to the TextBox in the form.
In the Thread Function just call the AddTextToTextBox("string")
function.
It First call to the Function, In that it check whether the thread has
the certain previlizes or not, If it has it directly add the text to
the textbox, Other wise it again assing the task to the main thread...

All the Best ...
I hope that .. This is inforamtion is reliable ..

On Nov 23, 8:11 am, "Le Minh" <hanami...@hotmail.comwrote:
I receive this message:
"System.InvalidOperationException: Cross-thread operation not valid: Control
[your_control_name_here] accessed from a thread other than the thread it was
created on"
...
this is a exception. Can fix it?
Nov 23 '06 #3

P: n/a
Kodali Ranganadh wrote:
In VS 2005 Desinging Microsoft Take care abot the major issue. Thread
Synchnization is also change a lot from the 2003 to the 2005 VS.
I don't believe that's true. MS has provided BackgroundWorker to make
things easier, but thread synchronization itself is just as it was
before, as far as I'm aware.
Actuall when the programm executed, system assign the one thread for
the entire GUI creation and take the changes on the GUI, that will be
[STA Thread]. That thread has the only permission to take the changes
and control the other GUI elements creataed by the STA thraed. If u
create a another threads with the use of System.Threadding.thread,
these thread are not have a enoff previliges to change the GUI elements
on the forms.
Well, it's not a case of *permissions* - it's a case of thread safety.
It's unsafe (and always has been under Windows, whether doing .NET or
not) to access a UI element except in a few very well-defined ways from
a thread other than the one running its message pump.

<snip>

Jon

Nov 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.