进度条工作但标签没有更新 [英] Progress bar working but label is not updating

查看:37
本文介绍了进度条工作但标签没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有接口的库.

I have a library with an Interface.

Public Interface Progress
{
    int ProgressValue{get;set;},
    string ProgressText{get;set;},
}

Library 有一个方法 Create(虚拟代码):

Library has a method Create (dummy code):

Public Class TestLibrary
{

    Progress _progress;

    Public void Create()
    {
        foreach(var n in TestList)
        {
            // Do Something
            _progress.ProgressValue = GetIndex(n);
            _progress.ProgressText = "Updating..." + n;
        }
    }
}

我有一个引用此库并调用 Create 方法的项目.它甚至实现了接口进程.

I have a project that references this library and calls Create method. It even Implements Interface Progress.

Public Class TestProject : Progress
{
    public int ProgressValue
    {
        get{return progressBar1.Value;}
        set{progressBar1.Value = value;}
    }

    public int ProgressText
    {
        get{return label1.Text;}
        set{label1.Text = value;}
    }
}

现在,当我运行应用程序时,进度条运行正常并正确显示进度,但 label1 的文本根本没有改变.但它确实在 for 循环结束时发生了变化,并显示了循环中的最后一项.有人能帮我解决这个问题吗?

Now when I run the application, Progress Bar behaves properly and shows the progress correctly, but the Text of label1 does not change at all. But it do change in the end of for loop and shows the last item in loop. Can anyone help me out in this?

注意:所有这些代码都是直接编写的,没有经过测试,因为我现在没有我的应用程序.抱歉有任何语法错误.

Note: All these codes are written directly without testing as I don't have my application now with me. Sorry for any syntax errors.

推荐答案

使用 Label 而不是 ProgressBar.你可以试试这个代码 [using BackGroundWorker] -

Used a Label instead of ProgressBar. You can try this code [using BackGroundWorker] -

using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        private BackgroundWorker _worker;
        BusinessClass _biz = new BusinessClass();
        public Form3()
        {
            InitializeComponent();
            InitWorker();
        }

        private void InitWorker()
        {
            if (_worker != null)
            {
                _worker.Dispose();
            }

            _worker = new BackgroundWorker
            {
                WorkerReportsProgress = true,
                WorkerSupportsCancellation = true
            };
            _worker.DoWork += DoWork;
            _worker.RunWorkerCompleted += RunWorkerCompleted;
            _worker.ProgressChanged += ProgressChanged;
            _worker.RunWorkerAsync();
        }


        void DoWork(object sender, DoWorkEventArgs e)
        {
            int highestPercentageReached = 0;
            if (_worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                double i = 0.0d;
                int junk = 0;
                for (i = 0; i <= 199990000; i++)
                {
                    int result = _biz.MyFunction(junk);
                    junk++;

                    // Report progress as a percentage of the total task.
                    var percentComplete = (int)(i / 199990000 * 100);
                    if (percentComplete > highestPercentageReached)
                    {
                        highestPercentageReached = percentComplete;
                        // note I can pass the business class result also and display the same in the LABEL  
                        _worker.ReportProgress(percentComplete, result);
                        _worker.CancelAsync();
                    }
                }

            }
        }

        void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                // Display some message to the user that task has been
                // cancelled
            }
            else if (e.Error != null)
            {
                // Do something with the error
            }
        }

        void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label1.Text =  string.Format("Result {0}: Percent {1}",e.UserState, e.ProgressPercentage);
        }
    }

    public class BusinessClass
    {
        public int MyFunction(int input)
        {
            return input+10;
        }
    }
}

几天前发布的相同内容这里

这篇关于进度条工作但标签没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆