Cross-thread operation not valid ???

  • Thread starter Thread starter Le Minh
  • Start date Start date
L

Le Minh

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?
 
Le said:
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
 
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 ..
 
Kodali said:
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
 
Back
Top