简体   繁体   English

使用backgroundworker的进度条

[英]progress bar using backgroundworker

There have been a bunch of questions on how to implement a progress bar and I believe I have been through all of them and I still don't know why my progress bar won't work. 关于如何实现进度条存在很多问题,我相信我已经经历了所有这些问题,但是我仍然不知道为什么我的进度条无法正常工作。

Well, it seems to work only after the operation is done, which is of course not what I want. 好吧,它似乎只有在操作完成后才能工作,这当然不是我想要的。 I want the progress bar to work as the main operation is doing its thing. 我希望进度条能够正常工作,因为主要操作正在执行它。

My code is pretty long and painful so I'm going to simplify it to make it as basic as possible. 我的代码很长而且很痛苦,因此我将对其进行简化以使其尽可能基本。

private void button1_Click(object sender, EventArgs e)
{
  backgroundWorker1.RunWorkerAsync();
  //Main UI Operation
}

private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
 for (int i = 1; i <= 100; i++)
  {
   // Wait 100 milliseconds.
   Thread.Sleep(100);
   // Report progress.
   backgroundWorker1.ReportProgress(i);     }
  }

private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs        e)
  {
   progressBar1.Value = e.ProgressPercentage;
  }

Like I said, I only see the progress bar start after my main UI operation. 就像我说的那样,我只在主UI操作之后看到进度条开始。

WorkerReportsProgress is set to True. WorkerReportsProgress设置为True。 Minimum and Maximum are 0 and 100 respectively. 最小值和最大值分别为0和100。

I feel like this might be a lack of understanding on Threads. 我觉得这可能是对线程缺乏了解。

I tested the same code as you and found it worked. 我测试了与您相同的代码,并发现它可以工作。 Did you forget to change the WorkerReportesProress property to True ? 您是否忘记将WorkerReportesProress属性更改为True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM