Search code examples
c#winformsprogress-baruilabel

Progress bar working but label is not updating


I have a library with an Interface.

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

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;
        }
    }
}

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;}
    }
}

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.


Solution

  • 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;
            }
        }
    }
    

    Posted the same a few days ago here