469,336 Members | 5,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Cross-thread operation not valid ???

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
3 30356
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

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
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.

Similar topics

3 posts views Thread by rollasoc | last post: by
1 post views Thread by Rob Woodworth | last post: by
6 posts views Thread by Bart Van der Donck | last post: by
6 posts views Thread by ampo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.