zoukankan      html  css  js  c++  java
  • WIN FORM 多线程更新UI(界面控件)

    方法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";
        }
    }
  • 相关阅读:
    剑指offer:复杂链表的复制
    剑值offer:最小的k个数
    剑指offer:第一个只出现一次的字符
    剑指offer:树的子结构
    leetcode 240搜索二维矩阵
    leetcode 22括号生成
    leetcode 79 单词搜索
    leetcode 17电话号码的字母组合
    leetcode 78子集
    leetcode 105从前序与中序遍历序列构造二叉树
  • 原文地址:https://www.cnblogs.com/anjou/p/2673103.html
Copyright © 2011-2022 走看看