none
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on. RRS feed

  • Question

  • I had this same problem and the "Invoke" fixed it.

    HOWEVER, I have a crap load of window controls that I'm changing.  Is it possible to put all of those control changes in a method, and then call the method in question using Invoke?

    txtBoxStatus.Invoke(new Action(() => txtBoxStatus.BackColor = Color.Green));//FIXED CODE
    //txtBoxStatus.BackColor = Color.Green;//OLD CODE

    So can I "Invoke" a Method? 

    With a parm? 

    With a Ref parm?

    Thanks in advance


    • Edited by HoyneDog Monday, April 2, 2018 8:15 PM
    Monday, April 2, 2018 8:04 PM

Answers

  • Hi,

       I have written code to do something similar to what you are asking. You pass in the control you want deal with and the method deals with the Invoke. Below is an example for changing text and one for enable and unenabling controls. You would want one for color.

            internal static void SetMessage(string message, Control control)
            {
                if (control.InvokeRequired)
                {
                    control.Parent.Invoke(new Action<string, Control>(SetMessage), message, control);
                    return;
                }
                control.Text = message;
            }
    
    
            internal static void SetEnable(bool enable, Control control)
            {
                if (control.InvokeRequired)
                {
                    control.Parent.Invoke(new Action<bool, Control>(SetEnable), enable, control);
                    return;
                }
                control.Enabled = enable;
            }

    Ethan


    Ethan Strauss

    • Marked as answer by HoyneDog Tuesday, April 3, 2018 11:32 AM
    Monday, April 2, 2018 8:56 PM

All replies

  • Hi,

       I have written code to do something similar to what you are asking. You pass in the control you want deal with and the method deals with the Invoke. Below is an example for changing text and one for enable and unenabling controls. You would want one for color.

            internal static void SetMessage(string message, Control control)
            {
                if (control.InvokeRequired)
                {
                    control.Parent.Invoke(new Action<string, Control>(SetMessage), message, control);
                    return;
                }
                control.Text = message;
            }
    
    
            internal static void SetEnable(bool enable, Control control)
            {
                if (control.InvokeRequired)
                {
                    control.Parent.Invoke(new Action<bool, Control>(SetEnable), enable, control);
                    return;
                }
                control.Enabled = enable;
            }

    Ethan


    Ethan Strauss

    • Marked as answer by HoyneDog Tuesday, April 3, 2018 11:32 AM
    Monday, April 2, 2018 8:56 PM
  • Ethan,

    Thanks so much for the code, I'm going to try it and I'll let you know

    Monday, April 2, 2018 11:00 PM
  • Yep.  SImple elegant.  THanks
    Monday, April 2, 2018 11:05 PM