如何使用WinForms进度条?

Dmy*_*tro 92 c# winforms progress-bar

我想显示在外部库中执行的计算进度.

例如,如果我有一些计算方法,并且我想在我的Form类中使用它为100000个值,我可以写:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }            

    private void Caluculate(int i)
    {
        double pow = Math.Pow(i, i);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        progressBar1.Maximum = 100000;
        progressBar1.Step = 1;

        for(int j = 0; j < 100000; j++)
        {
            Caluculate(j);
            progressBar1.PerformStep();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该在每次计算后执行步骤.但是,如果我在外部方法中执行所有100000计算,该怎么办?如果我不想让这个方法依赖于进度条,我什么时候应该"执行步骤"?例如,我可以写

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void CaluculateAll(System.Windows.Forms.ProgressBar progressBar)
    {
        progressBar.Maximum = 100000;
        progressBar.Step = 1;

        for(int j = 0; j < 100000; j++)
        {
            double pow = Math.Pow(j, j); //Calculation
            progressBar.PerformStep();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CaluculateAll(progressBar1);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不想这样做.

Pet*_*hie 110

我建议你看看BackgroundWorker.如果你的WinForm中有一个很大的循环,它会阻塞,你的应用程序看起来会被挂起.

查看BackgroundWorker.ReportProgress()如何将进度报告回UI线程.

例如:

private void Calculate(int i)
{
    double pow = Math.Pow(i, i);
}

private void button1_Click(object sender, EventArgs e)
{
    progressBar1.Maximum = 100;
    progressBar1.Step = 1;
    progressBar1.Value = 0;
    backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    var backgroundWorker = sender as BackgroundWorker;
    for (int j = 0; j < 100000; j++)
    {
        Calculate(j);
        backgroundWorker.ReportProgress((j * 100) / 100000);
    }
}

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

private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // TODO: do something with final calculation.
}
Run Code Online (Sandbox Code Playgroud)

  • 很好的例子,但代码中有一个小错误.您需要将backgroundWorker.WorkerReportsProgress设置为true.检查我的编辑 (5认同)
  • @mana 假设是“BackgroundWorker”是通过设计器添加并在那里配置的。但是是的,它需要配置为将 `WorkerReportsProgress` 设置为 `true`。 (2认同)
  • @mana 如果您想在进度中显示 100%,请在 `RunWorkerCompleted` 事件处理程序中执行此操作,如果您的 `DoWork` 处理程序没有执行此操作.. (2认同)

qua*_*oft 66

从.NET 4.5开始,您可以结合使用asyncawait with Progress来向UI线程发送更新:

private void Calculate(int i)
{
    double pow = Math.Pow(i, i);
}

public void DoWork(IProgress<int> progress)
{
    // This method is executed in the context of
    // another thread (different than the main UI thread),
    // so use only thread-safe code
    for (int j = 0; j < 100000; j++)
    {
        Calculate(j);

        // Use progress to notify UI thread that progress has
        // changed
        if (progress != null)
            progress.Report((j + 1) * 100 / 100000);
    }
}

private async void button1_Click(object sender, EventArgs e)
{
    progressBar1.Maximum = 100;
    progressBar1.Step = 1;

    var progress = new Progress<int>(v =>
    {
        // This lambda is executed in context of UI thread,
        // so it can safely update form controls
        progressBar1.Value = v;
    });

    // Run operation in another thread
    await Task.Run(() => DoWork(progress));

    // TODO: Do something after all calculations
}
Run Code Online (Sandbox Code Playgroud)

任务目前是实现目标的首选方式BackgroundWorker.

任务并Progress在此处详细说明:

  • 这应该是选定的答案,IMO。好答案! (2认同)
  • @RobertTausig确实,只有在WinForms的进度栏中才可以使用`Step`,而在这里并不需要它,但是它存在于问题的示例代码(标记为winforms)中,因此可能会保留下来。 (2认同)