zoukankan      html  css  js  c++  java
  • 委托在窗体中应用<4>

    1.Control的Invoke和BeginInvoke与委托的Invoke和BeginInvoke是2个概念,不能混淆
    2.Control的Invoke和BeginInvoke,他们的形参是delegate,委托的方法是在Control的线程上执行(即UI线程)
    以上第一点暂时没有看懂!
    直接上例子:
     public Form1()
            {
                InitializeComponent();
            }
            private delegate void ShowMessage();
            private Thread beginInvokeThread;
           // ShowMessage showMessage;
            private void button1_Click(object sender, EventArgs e)
            {
                this.richTextBox1.Text = "(1)进入按钮" + "
    ";
                button1.Invoke(new ShowMessage(ButtonMessage));
                this.richTextBox1.Text += "(3)已经点击" + "
    ";
    
                this.richTextBox2.Text = "(1)进入按钮" + "
    ";
                button1.BeginInvoke(new ShowMessage(ButtonMesage1));
                this.richTextBox2.Text += "(3)已经点击" + "
    ";
    
                this.richTextBox3.Text = "(1)进入按钮" + "
    ";
                beginInvokeThread = new Thread(new ThreadStart(ButtonMesage3));
                beginInvokeThread.Start();
                this.richTextBox3.Text += "(3)已经点击" + "
    ";
    
            }
    
            private void ButtonMessage()
            {
                this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.Text += "(2)点击按钮" + "
    ";
                    this.button1.BackColor = Color.Green;
                    this.button1.Text = "已经点击"+"
    ";
                }));
            }
    
            private void ButtonMesage1()
            {
                this.BeginInvoke(new Action(() => { this.richTextBox2.Text += "(2)点击按钮" + "
    "; }));
            }
            private void ButtonMesage3()
            {
                    richTextBox3.Invoke(new Action(() => { this.richTextBox3.Text += "(2)已经点击" + "
    "; }));
                    richTextBox3.BeginInvoke(new ShowMessage(BeginInvokeMethod));
                    richTextBox3.Invoke(new Action(() => { this.richTextBox3.Text += "(4)已经点击" + "
    "; }));
            }
            private void BeginInvokeMethod()
            {
                this.richTextBox3.Text += "(5)已经点击" + "
    ";
            }

    运行结果:

    这个实例说明了invoke,begininvoke的区别,以及在跨线程修改控件属性的应用;

  • 相关阅读:
    SQL跨服查询
    SQL时间函数
    MFC控件添加变量,control和value的区别
    error LNK2001 unresolved external symbol
    VS中C++代码折叠
    ERROR 2003 (HY000): Can't connect to MySQL server
    vs2012换肤功能,vs2012主题及自定义主题
    MFC、SDK和API有什么区别
    寻找子字符串int find_substr(char *s1, char *s2)
    document.title 跑马灯效果
  • 原文地址:https://www.cnblogs.com/xingyuanzier/p/13084662.html
Copyright © 2011-2022 走看看