使用ProgressBar加载C#winforms

时间:2017-05-23 16:35:54

标签: c# winforms

谁能告诉我为什么我的进度条没有移动?表格标题只是停留在“加载”状态。并且进度条停留在0%,它完成得很好并打开新表格,但进度条不移动,并且没有为绿色的任何条颜色着色?

这个过程需要10秒钟,所以每秒应该使条形图的绿色为10%,不是吗?

以下是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WordList_Master
{
    public partial class Loading : Form
    {
        public Loading()
        {
            InitializeComponent();

            progressBar1.Maximum = 100;
            progressBar1.Step = 1;
            progressBar1.Value = 0;

            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int lol = 100;
            while (lol > 0)
            {
                lol--;
                System.Threading.Thread.Sleep(100);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            this.Text = e.ProgressPercentage.ToString();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Form Form = new Form1();
            Form.Show();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您忘记了报告进度

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    int lol = 100;        
    while (lol > 0)
    {
        // report
        backgroundWorker1.ReportProgress(100-lol);

        lol--;
        System.Threading.Thread.Sleep(100);
    }
}

BackgroundWorker.ReportProgress in MSDN。此方法引发与后台工作程序关联的ProgressChanged事件(在UI线程上调用处理程序)。

刚刚意识到你也忘了注册事件处理程序:

backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

在你的构造函数中。

答案 1 :(得分:1)

确保您在某处连接事件处理程序,并确实按照@RenéVogt的建议报告了Progress。这应该有效:

public partial class Loading : Form
{
    public Loading()
    {
        InitializeComponent();

        progressBar1.Maximum = 100;
        progressBar1.Step = 1;
        progressBar1.Value = 0;

        backgroundWorker1.DoWork += backgroundWorker1_DoWork;
        backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
        backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int lol = 100;
        while (lol > 0)
        {
            lol--;
            backgroundWorker1.ReportProgress(100 - lol);
            System.Threading.Thread.Sleep(100);
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        this.Text = e.ProgressPercentage.ToString();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Form Form = new Form1();
        Form.Show();
    }
}