本文用三种办法实现progressBar从1到1000的进度演示。
方法1:
复制代码 01
int count = 1000; //测试总数 02
03

/**//// <summary> 04
/// 方法1 05
/// </summary> 06
private void setthread() 07

...{ 08
this.progressBar1.Minimum = 0; 09
this.progressBar1.Value = 0; 10
this.progressBar1.Maximum = count; 11
for (int i = 0; i < count; i++) 12

...{ 13
this.progressBar1.Value++; 14
Application.DoEvents(); 15
this.label2.Text = "总数"+count.ToString()+",剩余数量:" + Convert.ToString(count-i-1); 16
} 17
} 18

/**//// <summary> 19
/// 方法2 20
/// </summary> 21
private void setthread2() 22

...{ 23
this.progressBar1.Minimum = 0; 24
this.progressBar1.Value = 0; 25
this.progressBar1.Maximum = count; 26
for (int i = 0; i < count; i++) 27

...{ 28
this.progressBar1.Value++; 29
this.label2.Text = "总数" + count.ToString() + ",剩余数量:" + Convert.ToString(count - i - 1); 30
this.label2.Refresh(); 31
} 32
} 33

/**//// <summary> 34
/// 方法3,线程 35
/// </summary> 36
private void setthread3() 37

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

...{ 40
string tmptext = "总数" + count.ToString() + ",剩余数量:" + Convert.ToString(count - i - 1); 41
this.Invoke(new setLabel(setLabel2), tmptext); 42
this.Invoke(new setProgressBar(setProgressBar1),1); 43
} 44
} 45
private void button1_Click(object sender, EventArgs e) 46

...{ 47
setthread(); 48
} 49
private void button2_Click(object sender, EventArgs e) 50

...{ 51
setthread2(); 52
} 53
54
System.Threading.Thread thread; 55
private void button3_Click(object sender, EventArgs e) 56

...{ 57
this.progressBar1.Minimum = 0; 58
this.progressBar1.Value = 0; 59
this.progressBar1.Maximum = count; 60
61
System.Threading.Thread.CurrentThread.IsBackground = true; 62
thread = new System.Threading.Thread(new System.Threading.ThreadStart(setthread3)); 63
thread.Start(); 64
} 65
public delegate void setLabel(string text); 66
public delegate void setProgressBar(int value); 67
private void setLabel2(string text) 68

...{ 69
this.label2.Text = text; 70
} 71
private void setProgressBar1(int value) 72

...{ 73
this.progressBar1.Value = this.progressBar1.Value+value; 74
}
