zoukankan      html  css  js  c++  java
  • 跨线程更新控件UI 武胜

    private void btnCalculate_Click(object sender, EventArgs e)
            {
                Thread mythread = new Thread(Calculate);
                mythread.IsBackground = true;        //設置為後臺線程,程式關閉后進程也關閉,如果不設置true,則程式關閉,此線程還在內存,不會關閉
                mythread.Start();
            }
            private void Calculate()
            {
                this.Invoke(new Action(() => { progressBarWay.Visible = true; }));
                Stopwatch stopwatch = Stopwatch.StartNew();
                this.Invoke(new Action(() => { progressBarWay.Maximum = 500; }));
                for (int i = 0; i < 500; i++)
                {
                    Thread.Sleep(5);
                    this.Invoke(new Action(() => { progressBarWay.Value = i; }));
                }
                this.Invoke(new Action(() => { progressBarWay.Visible = false; }));
                stopwatch.Stop();
                long lSearchTime = stopwatch.ElapsedMilliseconds;
                MessageBox.Show(lSearchTime.ToString() + "毫秒");
            }

    http://www.codeproject.com/Articles/269787/Accessing-Windows-Forms-Controls-across-Threads

    delegate void ChangeMyTextDelegate(Control ctrl, string text);

    This defines a reference to a method with a return type of "void" and takes two parameters: Control and String. Take a look at the following method that tries to update the text of a specified control.

    public static void ChangeMyText(Control ctrl, string text);
    {
        ctrl.Text = text;
    }

    This is all well and good, assuming that the thread that calls this method is the same thread that created the control. If not, we risk having our cars crash into one another.

    To counter this, we use the Control's "InvokeRequired" property and the delegate we defined a moment ago. The resulting code looks like this:

    delegate void ChangeMyTextDelegate(Control ctrl, string text);
    public static void ChangeMyText(Control ctrl, string text)
    {
        if (ctrl.InvokeRequired)
        {
            ChangeMyTextDelegate del = new ChangeMyTextDelegate(ChangeMyText);
            ctrl.Invoke(del, ctrl, text)
        }
        else
        {
            ctrl.Text = text;
        }
    }
  • 相关阅读:
    bzoj4950
    P1377发奖金
    环信SDK与Apple Watch的结合(1)
    环信SDK与Apple Watch的结合(2)
    【Objective-C】0-第一个OC的类
    【Objective-C】4-空指针和野指针
    【Objective-C】3 -self关键字
    【Objective-C】2.自定义构造方法和description方法
    Swift函数|闭包
    Swift枚举|结构体|类|属性|方法|下标脚本|继承
  • 原文地址:https://www.cnblogs.com/zeroone/p/3048424.html
Copyright © 2011-2022 走看看