"C # uses the background to dynamically load data" Winform "anti-interface card dead" "BackgroundWorker" class

Source: Internet
Author: User

Using System.ComponentModel

Direct use of the Egprogressbar method

123456789101112131415161718192021222324252627282930313233343536373839   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  }

Pass-through method parameter RunWorkerAsync (parameter)

Get the e.argument of method DoWorkEventArgs

at the end of the decision DoWork method E.result automatic ? passed into the e.result of completed



From for notes (Wiz)

"C # uses the background to dynamically load data" Winform "anti-interface card dead" "BackgroundWorker" class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.