BeginInvoke 提高UI的响应速度

 代码
代码1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.ComponentModel;
using System.ComponentModel;4
 using System.Data;
using System.Data;5
 using System.Drawing;
using System.Drawing;6
 using System.Text;
using System.Text;7
 using System.Windows.Forms;
using System.Windows.Forms;8

9
 using System.Threading;
using System.Threading;10

11
 namespace CSharpGeriac
namespace CSharpGeriac12


 {
{13
 public partial class Form1 : Form
    public partial class Form1 : Form14

 
     {
{15
 public Form1()
        public Form1()16

 
         {
{17
 InitializeComponent();
            InitializeComponent();18

19
 Thread t = new Thread(new ThreadStart(ChangeLabel));
            Thread t = new Thread(new ThreadStart(ChangeLabel));20
 t.Start();
            t.Start();21
 }
        }22

23
 private void ChangeLabel()
        private void ChangeLabel()24

 
         {
{25
 for (int i = 0; i < 100; ++i)
            for (int i = 0; i < 100; ++i)26

 
             {
{27
 SetLabelText(i);
                SetLabelText(i);28
 Thread.Sleep(1000);
                Thread.Sleep(1000);29
 }
            }30
 }
        }31

32
 private void SetLabelText(int number)
        private void SetLabelText(int number)33

 
         {
{34
 // label.Text = number.ToString();
            // label.Text = number.ToString();35
 // Do NOT do this, as we are on a different thread.
            // Do NOT do this, as we are on a different thread.36

37
 // Check if we need to call BeginInvoke.
            // Check if we need to call BeginInvoke.38
 if (this.InvokeRequired)
            if (this.InvokeRequired)39

 
             {
{40
 // Pass the same function to BeginInvoke,
                // Pass the same function to BeginInvoke,41
 // but the call would come on the correct
                // but the call would come on the correct42
 // thread and InvokeRequired will be false.
                // thread and InvokeRequired will be false.43
 this.BeginInvoke(new SetLabelTextDelegate(SetLabelText),
                this.BeginInvoke(new SetLabelTextDelegate(SetLabelText),44

 new object[]
                                                 new object[]  { number });
{ number });45

46
 return;
                return;47
 }
            }48

49
 label1.Text = number.ToString();
            label1.Text = number.ToString();50

51
 }
        }52

53
 private delegate void SetLabelTextDelegate(int number);
        private delegate void SetLabelTextDelegate(int number);54
 }
    }55
 }
}
 别一例子
别一例子1
 using System;
using System;2
 using System.Drawing;
using System.Drawing;3
 using System.Threading;
using System.Threading;4
 using System.Windows.Forms;
using System.Windows.Forms;5

6
 class Program : Form
class Program : Form7


 {
{8
 private System.Windows.Forms.ProgressBar _ProgressBar;
    private System.Windows.Forms.ProgressBar _ProgressBar;9

10
 [STAThread]
    [STAThread]11
 static void Main()
    static void Main()12

 
     {
{13
 Application.Run(new Program());
        Application.Run(new Program());14
 }
    }15

16
 public Program()
    public Program()17

 
     {
{18
 InitializeComponent();
        InitializeComponent();19
 ThreadStart threadStart = Increment;
        ThreadStart threadStart = Increment;20
 threadStart.BeginInvoke(null, null);
        threadStart.BeginInvoke(null, null);21
 }
    }22

23
 void UpdateProgressBar()
    void UpdateProgressBar()24

 
     {
{25
 if (_ProgressBar.InvokeRequired)
        if (_ProgressBar.InvokeRequired)26

 
         {
{27
 MethodInvoker updateProgressBar = UpdateProgressBar;
            MethodInvoker updateProgressBar = UpdateProgressBar;28
 _ProgressBar.Invoke(updateProgressBar);
            _ProgressBar.Invoke(updateProgressBar);29
 }
        }30
 else
        else31

 
         {
{32
 _ProgressBar.Increment(1);
            _ProgressBar.Increment(1);33
 }
        }34
 }
    }35

36
 private void Increment()
    private void Increment()37

 
     {
{38
 for (int i = 0; i < 100; i++)
        for (int i = 0; i < 100; i++)39

 
         {
{40
 UpdateProgressBar();
            UpdateProgressBar();41
 Thread.Sleep(100);
            Thread.Sleep(100);42
 }
        }43
 if (InvokeRequired)
        if (InvokeRequired)44

 
         {
{45
 // Close cannot be called directly from
            // Close cannot be called directly from             46
 // a non-UI thread.
            // a non-UI thread.                                  47
 Invoke(new MethodInvoker(Close));
            Invoke(new MethodInvoker(Close));48
 }
        }49
 else
        else50

 
         {
{51
 Close();
            Close();52
 }
        }53
 }
    }54

55
 private void InitializeComponent()
    private void InitializeComponent()56

 
     {
{57
 _ProgressBar = new ProgressBar();
        _ProgressBar = new ProgressBar();58
 SuspendLayout();
        SuspendLayout();59

60
 _ProgressBar.Location = new Point(13, 17);
        _ProgressBar.Location = new Point(13, 17);61
 _ProgressBar.Size = new Size(267, 19);
        _ProgressBar.Size = new Size(267, 19);62

63
 ClientSize = new Size(292, 53);
        ClientSize = new Size(292, 53);64
 Controls.Add(this._ProgressBar);
        Controls.Add(this._ProgressBar);65
 Text = "Multithreading in Windows Forms";
        Text = "Multithreading in Windows Forms";66
 ResumeLayout(false);
        ResumeLayout(false);67
 }
    }68
 }
}.jpg) 
  
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号