Cleaning up code littered with InvokeRequired

前端 未结 8 1928
轮回少年
轮回少年 2020-12-04 09:16

I know that when manipulating UI controls from any non-UI thread, you must marshal your calls to the UI thread to avoid issues. The general consensus is that you should use

8条回答
  •  悲&欢浪女
    2020-12-04 10:02

    I cannot comment yet, hopefully someone will see this and add it to the accepted answer, which otherwise is spot on.

    control.Invoke(new Action(() => action(control))); should read
    control.Invoke(new Action(() => action(control)), null);

    As written the accepted answer will not compile because ISynchronizeInvoke.Invoke() does not have an overload with only 1 argument like Control.Invoke() does.

    One other thing is that the usage might be more clear as
    summary.InvokeIfRequired(c => { summary.Text = text; }); rather than as written summary.InvokeIfRequired(c => { textBox.Text = text });

提交回复
热议问题