标签.文本没有使用后台工作器更新

本文关键字:后台 工作 更新 文本 标签 | 更新日期: 2023-09-27 18:05:36

我使用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="XXXXXXXXXXX"以检查标签是否正在更新。我发现标签在循环期间仍然是"XXXXXXXXXXX",只是更新,直到循环完成。大约38秒后,xLabel。Text="Time: 00:38 sec"。

标签.文本没有使用后台工作器更新

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