locked
InvokeRequired for child controls RRS feed

  • Question

  • Hello.

    When updating controls on a multi-threaded application, we check the InvokeRequired property and use a invoke a delegate to update the controls if necessary.

    My question is, if a control has other controls in it, for example a StatusStrip that has a ToolStripStatusLabel and a ToolStripProgressBar, is it enough to check the InvokeRequired property on the StatusStrip, and update all the child controls, or do I need to check the InvokeRequired property for each child control individually?

    Thanks.

     

    Tuesday, April 26, 2011 4:57 AM

Answers

  • All the Controls and the Form are necessarily created in the same thread. The simpler is to use the InvokeRequired and the Invoke method from the Form. That way, the code is shorter if it resides within the Form's code.
    • Marked as answer by jaybeev Thursday, April 28, 2011 1:22 AM
    Tuesday, April 26, 2011 12:51 PM

All replies

  • There are controls which doesn't provide InvokeRequired  property, for such controls parent will be responsible for that . Which mean you call

    control.Parent.InvokeRequired. If control provides this property you have to call that exclusively.

    • Proposed as answer by Jack0x539 Tuesday, April 26, 2011 7:27 AM
    Tuesday, April 26, 2011 5:31 AM
  • Thank you.
    Tuesday, April 26, 2011 6:52 AM
  • when you need to change properties across threads, you are able to use the code below, 

     

    richTextBox1.Invoke(new Action(() =>

                        {

                            richTextBox1.Text += dr["ItemID"].ToString() + " - " + dr["ShortDesc"].ToString() + "\r\n";

                        }));

     

    with this you don't need to check the InvokeRequired property.


    The fastest methods aren't always the quicdkest methods

    MCPD : Windows & Web Developer
    Microsoft Certified Technology Specialist ( Windows & Web )

    @Gordon_Nologo
    Tuesday, April 26, 2011 7:10 AM
  • All the Controls and the Form are necessarily created in the same thread. The simpler is to use the InvokeRequired and the Invoke method from the Form. That way, the code is shorter if it resides within the Form's code.
    • Marked as answer by jaybeev Thursday, April 28, 2011 1:22 AM
    Tuesday, April 26, 2011 12:51 PM
  • when you need to change properties across threads, you are able to use the code below,

    richTextBox1.Invoke(new Action(() =>

                        {

                            richTextBox1.Text += dr["ItemID"].ToString() + " - " + dr["ShortDesc"].ToString() + "\r\n";

                        }));

     

    with this you don't need to check the InvokeRequired property.
    Yes, calling Invoke works even if you don't test InvokeRequired first. But it does some work that is useless if you're already working within the UI. Call Invoke only if InvokeRequired is true.
    Tuesday, April 26, 2011 12:52 PM
  • when you need to change properties across threads, you are able to use the code below,

    richTextBox1.Invoke(new Action(() =>

                        {

                            richTextBox1.Text += dr["ItemID"].ToString() + " - " + dr["ShortDesc"].ToString() + "\r\n";

                        }));

     

    with this you don't need to check the InvokeRequired property.
    Yes, calling Invoke works even if you don't test InvokeRequired first. But it does some work that is useless if you're already working within the UI. Call Invoke only if InvokeRequired is true.
    I normally call this from a Backgroundworker so invoke is always required.
    The fastest methods aren't always the quicdkest methods

    MCPD : Windows & Web Developer
    Microsoft Certified Technology Specialist ( Windows & Web )

    @Gordon_Nologo
    Tuesday, April 26, 2011 4:54 PM