System.Windows.Forms.Control.Invoke涓嶣eginInvoke

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了System.Windows.Forms.Control.Invoke涓嶣eginInvoke相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/bsp' title='bsp'>bsp   绾跨▼   ESS   mars   zh-cn   娑堟伅   close   鎶ラ敊   瀵硅薄   

WinForm鐨刄I瀵硅薄鍙兘鍦║I绾跨▼涓搷浣滐紝鍦ㄩ潪UI绾跨▼涓搷浣淯I瀵硅薄锛屼細寮曞彂涓嶅彲棰勭煡鐨勯敊璇紝杩欐椂灏遍渶瑕佺敤鍒癈ontrol.Invoke鎴栬€匔ontrol.BeginInvoke銆?/p>

鐢ㄦ埛绾跨▼璋冪敤Control.BeginInvoke浼氬悜UI娑堟伅闃熷垪鍙戦€佷竴涓甫濮旀墭娑堟伅锛孋ontrol.BeginInvoke涓嶄細闃诲鐢ㄦ埛绾跨▼锛岀洿鎺ヨ繑鍥濱AsyncResult瀵硅薄銆?/p>

鐢ㄦ埛绾跨▼璋冪敤Control.EndInvoke(IAsyncResult)锛孋ontrol.EndInvoke浼氶樆濉炵敤鎴风嚎绋嬶紝鐩村埌濮旀墭鎵ц瀹屾垚锛屽苟杩斿洖濮旀墭鐨勮繑鍥炲€笺€傛病鏈夎繑鍥炲€艰繑鍥瀗ull銆?/p>

Control.Invoke鐩稿綋浜嶤ontrol.BeginInvoke鍜孋ontrol.EndInvoke鐨勫悎浣擄紝浼氶樆濉炵敤鎴风嚎绋嬶紝鐩村埌濮旀墭鎵ц瀹屾垚锛屽苟杩斿洖濮旀墭鐨勮繑鍥炲€笺€傛病鏈夎繑鍥炲€艰繑鍥瀗ull銆?/p>

鏍规嵁绫荤户鎵垮叧绯伙紝鍦ㄧ獥鍙d腑鍙互鐩存帴浣跨敤BeginInvoke銆丒ndInvoke銆両nvoke銆?/p>

System.Object
??System.MarshalByRefObject
????System.ComponentModel.Component
??????System.Windows.Forms.Control
????????System.Windows.Forms.ScrollableControl
??????????System.Windows.Forms.ContainerControl
????????????System.Windows.Forms.Form

瀹為獙绀轰緥锛?/p>

绐椾綋锛?/p>

鎶€鏈垎浜浘鐗? src=

浠g爜锛?/p>

鎶€鏈垎浜浘鐗? id=
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 鍦ㄩ潪UI绾跨▼涓搷浣淯I瀵硅薄锛岃皟璇曡繍琛屼細鎶ラ敊銆?            // 鐩存帴杩愯浼氫娇绋嬪簭缃簬涓嶅彲棰勭煡椋庨櫓涔嬩腑銆?/span>
            new Thread(() =>
            {
                progressBar1.Value = 100;
            }).Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new Thread(() => {
                var result = this.Invoke(new Func<int, int, string>((n1, n2) =>
                {
                    for(int i = 0; i <= 100; i += 10)
                    {
                        progressBar1.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1+n2).ToString();
                }), 100, 21);
                MessageBox.Show(result.GetType().ToString() + ":" + result);
            }).Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                IAsyncResult asyncResult = this.BeginInvoke(new Func<int, int, string>((n1, n2) =>
                {
                    for (int i = 0; i <= 100; i += 10)
                    {
                        progressBar1.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1 + n2).ToString();
                }), 200, 32);
                MessageBox.Show("BeginInvoke涓嶄細闃诲");
                var result = this.EndInvoke(asyncResult);
                MessageBox.Show("EndInvoke浼氶樆濉烇紝" + result.GetType().ToString() + ":" + result);
            }).Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // 杩炵画缁欎袱涓鎵橈紝鐢变簬UI绾跨▼鍙湁涓€涓紝涓や釜濮旀墭鍙兘鍏堝悗鎵ц
            new Thread(() =>
            {
                IAsyncResult asyncResult1 = this.BeginInvoke(new Func<int, int, string>((n1, n2) =>
                {
                    for (int i = 0; i <= 100; i += 10)
                    {
                        progressBar1.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1 + n2).ToString();
                }), 200, 32);
                IAsyncResult asyncResult2 = this.BeginInvoke(new Func<int, int, string>((n1, n2) =>
                {
                    for (int i = 0; i <= 100; i += 10)
                    {
                        progressBar2.Value = i;
                        Thread.Sleep(200);
                    }
                    return (n1 + n2).ToString();
                }), 400, 64);
                MessageBox.Show("BeginInvoke涓嶄細闃诲");
                var result1 = this.EndInvoke(asyncResult1);
                MessageBox.Show("EndInvoke(asyncResult1)杩斿洖");
                var result2 = this.EndInvoke(asyncResult2);
                MessageBox.Show("EndInvoke(asyncResult2)杩斿洖");
                MessageBox.Show(
                    result1.GetType().ToString() + ":" + result1 + "
" +
                    result2.GetType().ToString() + ":" + result2);
            }).Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // 瑕佺瓑绮惧害鏉℃洿鏂板畬鎴愬悗锛岀偣鍑绘墠鑳藉搷搴?/span>
            MessageBox.Show("ha");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            progressBar2.Value = 0;
        }
    }
View Code

 







以上是关于System.Windows.Forms.Control.Invoke涓嶣eginInvoke的主要内容,如果未能解决你的问题,请参考以下文章