Label.text未使用backgroundworker进行更新

时间:2016-09-05 11:02:37

标签: c# winforms

我使用backgroundworker来显示循环计算的已用时间,以便完成循环内的繁重任务。

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

我分配了xLabel.Text =&#34; XXXXXXXXXXX&#34;以检查标签是否正在更新。我发现标签仍然是&#34; XXXXXXXXXXX&#34;在循环的持续时间内,只需更新直到循环结束。大约38秒后,xLabel.Text =&#34;时间:00:38秒&#34;。我该如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

xBackgroundWorker.RunWorkerAsync(); 

立即返回,不等到工人完成 (因为它是异步)

然后StopWatch_Summary.Stop();执行,因此DoWork中的循环在第一次迭代后完成

我想它应该看起来像这样(未经测试):

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