0

I have a winform app in the main form has a tabcontrol, one thread will create or remove tabpages very frequently.And some other background threads will get data and need to visit each tabpage's control inside it. the code is here and can work:

public void GetCustomerMessage()
            {
                    if (this.tabControl.InvokeRequired == true)
                    {
                        CustomerInforHandler handler = GetCustomerMessage;
                        this.tabControl.Invoke(handler);
                    }
                    else
                    {
                        //code to update something in some tabpages
                        //with foreach tabpage in tabcontrols and use BeginInvoke
                    }
              }

The problem is, I realize in fact the background thread need to visit the certain tabpage, not the tabcontrol, is it right check the InvokeRequired property of the tabcontrol, or I need to check each tabpage's InvokeRequired ? because sometimes I feel the UI is block for a short time.

Is it mean when the tabcontrol can visit UI thread, does not equals all the tabpage inside it can do this? or something can be optimized?

Joel Legaspi Enriquez
  • 1,236
  • 1
  • 10
  • 14
Bubble
  • 47
  • 1
  • 5
  • "Is it mean when the tabcontrol can visit UI thread, does not equals all the tabpage inside it can do this? or something can be optimized?" -- this question is not clear, could you paraphrase? – Joel Legaspi Enriquez Jul 24 '15 at 02:35
  • Just Invoke() against the Form via `this`. – Idle_Mind Jul 24 '15 at 02:44
  • tabpages belong to tabcontrols,and both tabcontrols and the tabpages are busy. When tabcontrols's InvokeRequired property is false, of course I can add or remove tabs on it.But does it means every tab's InvokeRequired property is also false(I can render something in a tabpage)? – Bubble Jul 24 '15 at 05:51
  • 1
    InvokeRequired has nothing to do with being "busy" or not. It tells you whether the currently running thread is NOT the same as the thread that created the control. All controls on a Form run in the SAME UI thread so you can Invoke against the Form via `this.Invoke` for any control on the Form. If the Form/main UI thread is "busy" however, then it cannot process the Invoke request because it is still executing some other code. Thus, if your Form is un-responsive, then you've got some long running code improperly being executed on the main UI thread. You have a bigger design-issue at hand. – Idle_Mind Jul 24 '15 at 17:28
  • thank you for your clear and useful explanation. Also I find the reason is too many places do not use the `InvokeBegin`, just use `Invoke` to do large database operation lead the UI block. – Bubble Jul 28 '15 at 03:02

0 Answers0