按照惯例先上个图,自己照着人家的例子写了一遍,基本上把这个弄懂了,这个控件就是省事,不懂线程原理的人也可使,ms就是厉害,生产力节节提高哈,
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading;
9using System.Windows.Forms;
10
11namespace BrowserDemo
12{
13 public partial class Form7 : Form
14 {
15 const int argument = 20000;
16
17 public Form7()
18 {
19 InitializeComponent();
20 }
21
22 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
23 {
24 for(var i=0;i<int.Parse(e.Argument.ToString());i++)
25 {
26 e.Result = i;
27 (sender as BackgroundWorker).ReportProgress((int)(((double)i / (double)((int)e.Argument)) * 100),i);
28 Thread.Sleep(20);
29 if (this.backgroundWorker1.CancellationPending)
30 return;
31 }
32 }
33
34 private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
35 {
36 this.listBox1.Items.Add(e.UserState);
37 this.progressBar1.Value = e.ProgressPercentage;
38 }
39
40 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
41 {
42 MessageBox.Show("finished");
43 }
44
45 private void button1_Click(object sender, EventArgs e)
46 {
47 if (this.backgroundWorker1.IsBusy)
48 {
49 MessageBox.Show("慢则那,等着吧");
50 return;
51 }
52 this.backgroundWorker1.RunWorkerAsync(argument);
53 this.button2.Enabled = true;
54 this.button3.Enabled = true;
55
56 }
57
58 private void button2_Click(object sender, EventArgs e)
59 {
60 MessageBox.Show("111");
61 }
62
63 private void button3_Click(object sender, EventArgs e)
64 {
65 this.backgroundWorker1.CancelAsync();
66 }
67
68 private void Form7_Load(object sender, EventArgs e)
69 {
70 this.button2.Enabled = false;
71 this.button3.Enabled = false;
72 }
73 }
74}
75
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading;
9using System.Windows.Forms;
10
11namespace BrowserDemo
12{
13 public partial class Form7 : Form
14 {
15 const int argument = 20000;
16
17 public Form7()
18 {
19 InitializeComponent();
20 }
21
22 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
23 {
24 for(var i=0;i<int.Parse(e.Argument.ToString());i++)
25 {
26 e.Result = i;
27 (sender as BackgroundWorker).ReportProgress((int)(((double)i / (double)((int)e.Argument)) * 100),i);
28 Thread.Sleep(20);
29 if (this.backgroundWorker1.CancellationPending)
30 return;
31 }
32 }
33
34 private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
35 {
36 this.listBox1.Items.Add(e.UserState);
37 this.progressBar1.Value = e.ProgressPercentage;
38 }
39
40 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
41 {
42 MessageBox.Show("finished");
43 }
44
45 private void button1_Click(object sender, EventArgs e)
46 {
47 if (this.backgroundWorker1.IsBusy)
48 {
49 MessageBox.Show("慢则那,等着吧");
50 return;
51 }
52 this.backgroundWorker1.RunWorkerAsync(argument);
53 this.button2.Enabled = true;
54 this.button3.Enabled = true;
55
56 }
57
58 private void button2_Click(object sender, EventArgs e)
59 {
60 MessageBox.Show("111");
61 }
62
63 private void button3_Click(object sender, EventArgs e)
64 {
65 this.backgroundWorker1.CancelAsync();
66 }
67
68 private void Form7_Load(object sender, EventArgs e)
69 {
70 this.button2.Enabled = false;
71 this.button3.Enabled = false;
72 }
73 }
74}
75
完整的工程如下
https://files.cnblogs.com/lexus/BrowserDemo.7z
相关资源,发现msdn上写得比较还好,不过自己写一遍自己就清楚了,我发现网上的msdn,是不是和vs2008里的光盘里的那个msdn不一样,有些资源为什么在光盘上的找不到呢,等我采集程序做好了,我要把它都抓下来,占为已有,嘿嘿
http://msdn.microsoft.com/zh-cn/library/ms233672(VS.80).aspx