方法1,更新单个控件:
public delegate void ControlTextMethod(Control control, string text);
private void SetControlText(Control control, string text)
{
    if (this.InvokeRequired)
    {
        ControlTextMethod controlTextMethod = new ControlTextMethod(SetControlText);
        this.Invoke(controlTextMethod, new object[] { control, text });
    }
    else
    {
        control.Text = text;
    }
}

需要更新控件的Text的地方,直接调用SetControlText方法就可以了。

 

方法2,使用“UIThread”:

public void UIThread(MethodInvoker method)
{
    if (this.InvokeRequired)
    {
        this.Invoke(method);
    }
    else
    {
        method.Invoke();
    }
}

public void UpdateUI()
{
    this.UIThread(delegate
    {
        this.Label1.Text = "msg1";
        this.Label2.Text = "msg2";
    });
}

在需要更新界面的地方这么调用:this.UIThread(delegate{this.Label1.Text="msg1";...});。

 

方法3,在一个方法里集中更新多个控件:

public void UpdateUI()
{
    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(delegate { UpdateUI(); }));
    }
    else
    {
        this.Label.Text = "msg1";
        this.Labe2.Text = "msg2";
    }
}

 

 

相关文章:

  • 2021-10-12
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
  • 2021-08-05
  • 2021-12-13
  • 2021-12-05
  • 2022-02-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案