How to determine when the user control is fully loaded and shown?

后端 未结 3 394
一向
一向 2020-12-16 02:13

There were already a few similar questions on stackoverflow, but I haven\'t found the answer

I have an application that consists of several tab pages. On one of them

3条回答
  •  遥遥无期
    2020-12-16 02:57

    In order to make the UI more responsive, you should post yourself a message (Control.BeginInvoke), do one operation, post yourself another message. Then every time you do anything, the next step will get queued after all user messages, so user actions will get processed promptly.

    One really nifty approach is to use yield return and let the compiler take care of all the closures logic:

    IEnumerable AsyncLoadUI()
    {
        var p = new Panel();
        Controls.Add(p);
        yield return null;
    
        for( int i = 0; i < 50; ++i ) {
            var txt = new TextBox();
            p.Controls.Add(txt);
            yield return null;
        }
    }
    
    override void OnLoad(EventArgs e)
    {
        IEnumerator tasks = AsyncLoadUI().GetEnumerator();
        MethodInvoker msg = null;
        msg = delegate { if (tasks.MoveNext()) BeginInvoke(msg); };
        msg();
    }
    

提交回复
热议问题