标签:
using System.ComponentModel
直接使用EgProgressBar方法
BackgroundWorker Bw = new BackgroundWorker(); ListView listView1 = new ListView(); public void EgProgressBar() { DirectoryInfo di = new DirectoryInfo(@"C:/Windows"); FileInfo[] fi = di.GetFiles(); progressBar1.Maximum = fi.Length - 1; //设置进度条的最大值 Bw.WorkerSupportsCancellation = true; Bw.WorkerReportsProgress = true; Bw.DoWork += new DoWorkEventHandler(Add); //绑定事件 Bw.ProgressChanged += new ProgressChangedEventHandler(Progress); Bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(End); Bw.RunWorkerAsync(); } public void Add(object sender, DoWorkEventArgs e) { DirectoryInfo di = new DirectoryInfo(@"C:/Windows"); foreach (FileInfo item in di.GetFiles()) { ListViewItem lvt = new ListViewItem();//用listviewItem来存储获取到文件的信息 lvt.Text = item.FullName; lvt.SubItems.Add(DateTime.Now.ToLongTimeString()); Thread.Sleep(500); Bw.ReportProgress(listView1.Items.Count, lvt);//把当前的进度报告出去,获取到第几个文件和该文件的信息 } } public void Progress(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage;//获取第几个文件,用来改变进度条的进度 ListViewItem lv = e.UserState as ListViewItem; listView1.Items.Add(lv);//把最新获取到的文件信息添加到listview } public void End(object sender, RunWorkerCompletedEventArgs e) { progressBar1.Value = 0;//进度条清0 }
传递方法参数RunWorkerAsync(参数)
获取方法 DoWorkEventArgs 的 e.Argument
结束时判定 Dowork方法 e.result 自动传递到completed 的e.result 中
【转】【C#利用后台动态加载数据】Winform“防界面卡死”【BackgroundWorker】类
标签:
原文地址:http://www.cnblogs.com/yzpopulation/p/4499029.html