本文介绍了无法获得带有代码的进度栏动画,progresschanged未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在此处编写代码

i'm trying to do the code here

http://www.dotnetperls.com/progressbar

这是我的代码.我聘请了一名背景工作人员,并以编程方式添加了进度条.

Here is the code I have. I've drawn on a backgroundworker, and programmatically added a progress bar.

我已经尝试过执行代码,并且已经尝试使用消息框查看正在发生的情况,并且看起来它只执行了for循环的一次迭代,这是针对i = 0的情况,以及看来它是从此完成的过程,而Progress过程永远不会被调用.

I've tried stepping over the code, and i've tried using messageboxes to see what is going on, and it looks like it is only executing one iteration of the for loop, which is for when i=0, and it seems it goes from that, to te done procedure, and the Progress procedure never gets invoked.

进度栏永远不会将值从0更改.

And the progress bar never changes value from 0.

当我希望它升至100时.

When I want it to progress to 100.

我确实尝试注释掉program.cs中的//Application.EnableVisualStyles(); 这一行,以删除progerss栏具有注释或未注释的自然动画,但jt没有即使运行for循环的每个迭代,它在i = 0之后也会退出.而且backgroundworker或进度条没有进度.

I did try commenting out this line //Application.EnableVisualStyles(); in program.cs to remove a natural animation that the progerss bar has, but either way, commented or uncommented, jt's not even running every iteration of the for loop, it's quitting after i=0.. And there is no progress with the backgroundworker, or the progress bar.

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;
using System.Threading;

namespace backgroundworker2
{
    public partial class Form1 : Form
    {
        ProgressBar progressBar1 = new ProgressBar();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
//            progressBar1.BackColor = Color.Red;
            this.Controls.Add(progressBar1);          

            //progressBar1.Value = 100;
                backgroundWorker1.RunWorkerAsync();



        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

           // textBox1.Text = "0";
            for (int i = 0; i < 100; i++)
            {
           //     MessageBox.Show("a"+i);
                Thread.Sleep(1000);
                backgroundWorker1.ReportProgress(i);
            }


        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("done");
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
           // textBox1.Text = Convert.ToString( e.ProgressPercentage);
            progressBar1.Value = e.ProgressPercentage;
           // MessageBox.Show("asdf");
        }
    }

}

推荐答案

Form1_Load 中,您运行 BackgroundWorker ,但尚未设置它为 WorkerReportsProgress 属性设置为 true .默认值为 false .因此,在调用 RunWorkerAsync()方法之前,需要添加以下行:

In Form1_Load you run the BackgroundWorker but you haven't set it's WorkerReportsProgress property to true. The default is false. So you need to add this line before you call the RunWorkerAsync() method:

backgroundWorker1.WorkerReportsProgress = true;

这篇关于无法获得带有代码的进度栏动画,progresschanged未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:54