Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
I get the following error
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on.
This is a callback from a wcf.
I have a textBox and I need to update the value and appendtext to it. This value is coming back from another thread and updates the UI.
public CarStatus CarState
{
get
{
return _carState;
}
set
{
开发者_如何学运维 _carState;= value;
CarStatus tmpCarState;=null;
if (txtResult.InvokeRequired)
{
txtResult.Invoke(new MethodInvoker(() => { tmpCarState;=null;= _carState;}));
}
txtResult.AppendText(string.Format("Car status is: {0}{1}", tmpCarState, Environment.NewLine));
}
the following crashes!!
You forgot the else
, as you're updating the text of the control via AppendText
always, not just on non-invoke required.
And, well, I think you've got something wrong here: You're setting member variables through the invoker, but changing the actual WinForm component on any thread? You probably just want to but the whole block on Invoke.
精彩评论