Vincent
Vincent

Reputation: 29

Label.text is not updating using backgroundworker

I'm using backgroundworker to shows elapsed time for the loop to compute in order to finished a heavy task inside the loop.

    namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        Stopwatch StopWatch_Summary = new Stopwatch();
        BackgroundWorker xBackgroundWorker = new BackgroundWorker();
        Label xLabel = new Label();

        public Form1()
        {
            InitializeComponent();

            xBackgroundWorker.WorkerReportsProgress = true;
            xBackgroundWorker.DoWork += xbackgroundWorker_DoWork;
            xBackgroundWorker.ProgressChanged += xbackgroundWorker_ProgressChanged;
            xLabel.Text = "XXXXXXXXXXXXX";

            HeavyComputation();
        }

        private void xbackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                xBackgroundWorker.ReportProgress(0);
                System.Threading.Thread.Sleep(1000);
                if (!StopWatch_Summary.IsRunning)
                {
                    break;
                }
            }
        }
        private void xbackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            TimeSpan timeSpan = StopWatch_Summary.Elapsed;

            xLabel.Text = String.Format("Time : {0:00}:{1:00} sec", timeSpan.Minutes, timeSpan.Seconds);
        }
        private void HeavyComputation()
        {
            StopWatch_Summary.Start();
            xBackgroundWorker.RunWorkerAsync();

            //for(int i=1;i<=MyX;i++)
            //{
            //Heavy Computation that takes 38seconds to compute
            //}

            StopWatch_Summary.Stop();
        }
    }
}

I assigned xLabel.Text="XXXXXXXXXXX" in order to check if label is updating. I found out that label remains "XXXXXXXXXXX" during the duration of loop and just update until loop is finished. after roughly 38seconds the xLabel.Text="Time : 00:38 sec". How can i fixed this.

Upvotes: 0

Views: 69

Answers (1)

doerig
doerig

Reputation: 1857

xBackgroundWorker.RunWorkerAsync(); 

returns immediately and does not wait until the worker has finished (because it is Async)

then StopWatch_Summary.Stop(); is executed and therefore the loop in DoWork is finished after the first iteration

I guess it should look like this (not tested):

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        Stopwatch StopWatch_Summary = new Stopwatch();
        BackgroundWorker xBackgroundWorker = new BackgroundWorker();
        Label xLabel = new Label();

        public Form1()
        {
            InitializeComponent();

            xBackgroundWorker.WorkerReportsProgress = true;
            xBackgroundWorker.DoWork += xbackgroundWorker_DoWork;
            xBackgroundWorker.ProgressChanged += xbackgroundWorker_ProgressChanged;
            xLabel.Text = "XXXXXXXXXXXXX";

            StartHeavyComputation();
        }

        private void xbackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            StopWatch_Summary.Start();
            xBackgroundWorker.ReportProgress(0);

            for(int i=1;i<=MyX;i++)
            {
                xBackgroundWorker.ReportProgress(i);    
                //Heavy Computation that takes 38seconds to compute
            }

            StopWatch_Summary.Stop();
        }

        private void xbackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            TimeSpan timeSpan = StopWatch_Summary.Elapsed;
            xLabel.Text = String.Format("Time : {0:00}:{1:00} sec", timeSpan.Minutes, timeSpan.Seconds);
        }

        private void StartHeavyComputation()
        {
            xBackgroundWorker.RunWorkerAsync();       
        }
    }
}

Upvotes: 2

Related Questions