tags:

views:

62

answers:

1

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

+2  A: 

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.

gimpf
Thanks for your reply.It sort of works like thisBeginInvoke(new MethodInvoker(() =>txtCarState.AppendText(_carState));is that what you mean?
Yes. 9876543210
gimpf