BeginInvoke 提高UI的响应速度

ContractedBlock.gif ExpandedBlockStart.gif 代码
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.ComponentModel;
 4None.gifusing System.Data;
 5None.gifusing System.Drawing;
 6None.gifusing System.Text;
 7None.gifusing System.Windows.Forms;
 8None.gif
 9None.gifusing System.Threading;
10None.gif
11None.gifnamespace CSharpGeriac
12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
13InBlock.gif    public partial class Form1 : Form
14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
15InBlock.gif        public Form1()
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            InitializeComponent();
18InBlock.gif
19InBlock.gif            Thread t = new Thread(new ThreadStart(ChangeLabel));
20InBlock.gif            t.Start();
21ExpandedSubBlockEnd.gif        }

22InBlock.gif
23InBlock.gif        private void ChangeLabel()
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            for (int i = 0; i < 100++i)
26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
27InBlock.gif                SetLabelText(i);
28InBlock.gif                Thread.Sleep(1000);
29ExpandedSubBlockEnd.gif            }

30ExpandedSubBlockEnd.gif        }

31InBlock.gif
32InBlock.gif        private void SetLabelText(int number)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            // label.Text = number.ToString();
35InBlock.gif            // Do NOT do this, as we are on a different thread.
36InBlock.gif
37InBlock.gif            // Check if we need to call BeginInvoke.
38InBlock.gif            if (this.InvokeRequired)
39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
40InBlock.gif                // Pass the same function to BeginInvoke,
41InBlock.gif                // but the call would come on the correct
42InBlock.gif                // thread and InvokeRequired will be false.
43InBlock.gif                this.BeginInvoke(new SetLabelTextDelegate(SetLabelText),
44ExpandedSubBlockStart.gifContractedSubBlock.gif                                                 new object[] dot.gif{ number });
45InBlock.gif
46InBlock.gif                return;
47ExpandedSubBlockEnd.gif            }

48InBlock.gif
49InBlock.gif            label1.Text = number.ToString();
50InBlock.gif
51ExpandedSubBlockEnd.gif        }

52InBlock.gif
53InBlock.gif        private delegate void SetLabelTextDelegate(int number);
54ExpandedSubBlockEnd.gif    }

55ExpandedBlockEnd.gif}
Another Example:
ContractedBlock.gif ExpandedBlockStart.gif 别一例子
 1None.gifusing System;
 2None.gifusing System.Drawing;
 3None.gifusing System.Threading;
 4None.gifusing System.Windows.Forms;
 5None.gif
 6None.gifclass Program : Form
 7ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 8InBlock.gif    private System.Windows.Forms.ProgressBar _ProgressBar;
 9InBlock.gif
10InBlock.gif    [STAThread]
11InBlock.gif    static void Main()
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        Application.Run(new Program());
14ExpandedSubBlockEnd.gif    }

15InBlock.gif
16InBlock.gif    public Program()
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18InBlock.gif        InitializeComponent();
19InBlock.gif        ThreadStart threadStart = Increment;
20InBlock.gif        threadStart.BeginInvoke(nullnull);
21ExpandedSubBlockEnd.gif    }

22InBlock.gif
23InBlock.gif    void UpdateProgressBar()
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25InBlock.gif        if (_ProgressBar.InvokeRequired)
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            MethodInvoker updateProgressBar = UpdateProgressBar;
28InBlock.gif            _ProgressBar.Invoke(updateProgressBar);
29ExpandedSubBlockEnd.gif        }

30InBlock.gif        else
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            _ProgressBar.Increment(1);
33ExpandedSubBlockEnd.gif        }

34ExpandedSubBlockEnd.gif    }

35InBlock.gif
36InBlock.gif    private void Increment()
37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
38InBlock.gif        for (int i = 0; i < 100; i++)
39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
40InBlock.gif            UpdateProgressBar();
41InBlock.gif            Thread.Sleep(100);
42ExpandedSubBlockEnd.gif        }

43InBlock.gif        if (InvokeRequired)
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            // Close cannot be called directly from             
46InBlock.gif            // a non-UI thread.                                  
47InBlock.gif            Invoke(new MethodInvoker(Close));
48ExpandedSubBlockEnd.gif        }

49InBlock.gif        else
50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
51InBlock.gif            Close();
52ExpandedSubBlockEnd.gif        }

53ExpandedSubBlockEnd.gif    }

54InBlock.gif
55InBlock.gif    private void InitializeComponent()
56ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
57InBlock.gif        _ProgressBar = new ProgressBar();
58InBlock.gif        SuspendLayout();
59InBlock.gif
60InBlock.gif        _ProgressBar.Location = new Point(1317);
61InBlock.gif        _ProgressBar.Size = new Size(26719);
62InBlock.gif
63InBlock.gif        ClientSize = new Size(29253);
64InBlock.gif        Controls.Add(this._ProgressBar);
65InBlock.gif        Text = "Multithreading in Windows Forms";
66InBlock.gif        ResumeLayout(false);
67ExpandedSubBlockEnd.gif    }

68ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值