“Cross-thread operation not valid” exception on inner controls

前端 未结 7 1482
南方客
南方客 2020-12-19 05:36

I\'ve been struggling with this for quite a while: I have a function designed to add control to a panel with cross-thread handling, the problem is that though the panel and

7条回答
  •  轮回少年
    2020-12-19 06:04

    As an aside - to save yourself having to create countless delegate types:

    if (panel.InvokeRequired)
    {
        panel.Invoke((MethodInvoker) delegate { AddControlToPanel(panel,ctrl); } );
        return;
    }
    

    Additionally, this now does regular static checks on the inner call to AddControlToPanel, so you can't get it wrong.

提交回复
热议问题