按照惯例先上个图,自己照着人家的例子写了一遍,基本上把这个弄懂了,这个控件就是省事,不懂线程原理的人也可使,ms就是厉害,生产力节节提高哈,

自写的BackgroundWorker的学习例子

 

 1自写的BackgroundWorker的学习例子using System;
 2自写的BackgroundWorker的学习例子using System.Collections.Generic;
 3自写的BackgroundWorker的学习例子using System.ComponentModel;
 4自写的BackgroundWorker的学习例子using System.Data;
 5自写的BackgroundWorker的学习例子using System.Drawing;
 6自写的BackgroundWorker的学习例子using System.Linq;
 7自写的BackgroundWorker的学习例子using System.Text;
 8自写的BackgroundWorker的学习例子using System.Threading;
 9自写的BackgroundWorker的学习例子using System.Windows.Forms;
10自写的BackgroundWorker的学习例子
11自写的BackgroundWorker的学习例子namespace BrowserDemo
12自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子{
13自写的BackgroundWorker的学习例子    public partial class Form7 : Form
14自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子    自写的BackgroundWorker的学习例子{
15自写的BackgroundWorker的学习例子        const int argument = 20000;
16自写的BackgroundWorker的学习例子
17自写的BackgroundWorker的学习例子        public Form7()
18自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
19自写的BackgroundWorker的学习例子            InitializeComponent();
20自写的BackgroundWorker的学习例子        }

21自写的BackgroundWorker的学习例子
22自写的BackgroundWorker的学习例子        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
23自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
24自写的BackgroundWorker的学习例子            for(var i=0;i<int.Parse(e.Argument.ToString());i++)
25自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子            自写的BackgroundWorker的学习例子{
26自写的BackgroundWorker的学习例子                e.Result = i;
27自写的BackgroundWorker的学习例子                (sender as BackgroundWorker).ReportProgress((int)(((double)i / (double)((int)e.Argument)) * 100),i);
28自写的BackgroundWorker的学习例子                Thread.Sleep(20);
29自写的BackgroundWorker的学习例子                if (this.backgroundWorker1.CancellationPending)
30自写的BackgroundWorker的学习例子                    return;
31自写的BackgroundWorker的学习例子            }

32自写的BackgroundWorker的学习例子        }

33自写的BackgroundWorker的学习例子
34自写的BackgroundWorker的学习例子        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
35自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
36自写的BackgroundWorker的学习例子            this.listBox1.Items.Add(e.UserState);
37自写的BackgroundWorker的学习例子            this.progressBar1.Value = e.ProgressPercentage;
38自写的BackgroundWorker的学习例子        }

39自写的BackgroundWorker的学习例子
40自写的BackgroundWorker的学习例子        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
41自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
42自写的BackgroundWorker的学习例子            MessageBox.Show("finished");
43自写的BackgroundWorker的学习例子        }

44自写的BackgroundWorker的学习例子
45自写的BackgroundWorker的学习例子        private void button1_Click(object sender, EventArgs e)
46自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
47自写的BackgroundWorker的学习例子            if (this.backgroundWorker1.IsBusy)
48自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子            自写的BackgroundWorker的学习例子{
49自写的BackgroundWorker的学习例子                MessageBox.Show("慢则那,等着吧");
50自写的BackgroundWorker的学习例子                return;
51自写的BackgroundWorker的学习例子            }

52自写的BackgroundWorker的学习例子            this.backgroundWorker1.RunWorkerAsync(argument);
53自写的BackgroundWorker的学习例子            this.button2.Enabled = true;
54自写的BackgroundWorker的学习例子            this.button3.Enabled = true;
55自写的BackgroundWorker的学习例子
56自写的BackgroundWorker的学习例子        }

57自写的BackgroundWorker的学习例子
58自写的BackgroundWorker的学习例子        private void button2_Click(object sender, EventArgs e)
59自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
60自写的BackgroundWorker的学习例子            MessageBox.Show("111");
61自写的BackgroundWorker的学习例子        }

62自写的BackgroundWorker的学习例子
63自写的BackgroundWorker的学习例子        private void button3_Click(object sender, EventArgs e)
64自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
65自写的BackgroundWorker的学习例子            this.backgroundWorker1.CancelAsync();
66自写的BackgroundWorker的学习例子        }

67自写的BackgroundWorker的学习例子
68自写的BackgroundWorker的学习例子        private void Form7_Load(object sender, EventArgs e)
69自写的BackgroundWorker的学习例子自写的BackgroundWorker的学习例子        自写的BackgroundWorker的学习例子{
70自写的BackgroundWorker的学习例子            this.button2.Enabled = false;
71自写的BackgroundWorker的学习例子            this.button3.Enabled = false;
72自写的BackgroundWorker的学习例子        }

73自写的BackgroundWorker的学习例子    }

74自写的BackgroundWorker的学习例子}

75自写的BackgroundWorker的学习例子

 

完整的工程如下

 

 http://files.cnblogs.com/lexus/BrowserDemo.7z

 

相关资源,发现msdn上写得比较还好,不过自己写一遍自己就清楚了,我发现网上的msdn,是不是和vs2008里的光盘里的那个msdn不一样,有些资源为什么在光盘上的找不到呢,等我采集程序做好了,我要把它都抓下来,占为已有,嘿嘿

 http://msdn.microsoft.com/zh-cn/library/ms233672(VS.80).aspx

相关文章: