Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Label.text未使用backgroundworker更新_C#_Winforms - Fatal编程技术网

C# Label.text未使用backgroundworker更新

C# Label.text未使用backgroundworker更新,c#,winforms,C#,Winforms,我使用backgroundworker来显示循环计算所用的时间,以便完成循环内的繁重任务 namespace WindowsFormsApplication3 { public partial class Form1 : Form { Stopwatch StopWatch_Summary = new Stopwatch(); BackgroundWorker xBackgroundWorker = new BackgroundWorker

我使用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();
        }
    }
}
命名空间窗口窗体应用程序3
{
公共部分类Form1:Form
{
秒表秒表汇总=新秒表();
BackgroundWorker xBackgroundWorker=新的BackgroundWorker();
标签xLabel=新标签();
公共表格1()
{
初始化组件();
xBackgroundWorker.WorkerReportsProgress=true;
xBackgroundWorker.DoWork+=xBackgroundWorker\u DoWork;
xBackgroundWorker.ProgressChanged+=xBackgroundWorker\u ProgressChanged;
xLabel.Text=“xxxxxxxxxxxx”;
重计算();
}
私有void xbackgroundWorker_DoWork(对象发送方,DoWorkEventArgs e)
{
while(true)
{
xBackgroundWorker.ReportProgress(0);
系统线程线程睡眠(1000);
如果(!秒表\u摘要正在运行)
{
打破
}
}
}
私有void xbackgroundWorker_ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
TimeSpan TimeSpan=秒表_Summary.appead;
xLabel.Text=String.Format(“时间:{0:00}:{1:00}秒”,timeSpan.Minutes,timeSpan.Seconds);
}
私有无效重计算()
{
秒表_Summary.Start();
xBackgroundWorker.RunWorkerAsync();
//对于(int i=1;i
立即返回,不等待工作人员完成
(因为它是异步的)

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

我想应该是这样的(未测试):

命名空间窗口窗体应用程序3
{
公共部分类Form1:Form
{
秒表秒表汇总=新秒表();
BackgroundWorker xBackgroundWorker=新的BackgroundWorker();
标签xLabel=新标签();
公共表格1()
{
初始化组件();
xBackgroundWorker.WorkerReportsProgress=true;
xBackgroundWorker.DoWork+=xBackgroundWorker\u DoWork;
xBackgroundWorker.ProgressChanged+=xBackgroundWorker\u ProgressChanged;
xLabel.Text=“xxxxxxxxxxxx”;
启动AVY计算();
}
私有void xbackgroundWorker_DoWork(对象发送方,DoWorkEventArgs e)
{
秒表_Summary.Start();
xBackgroundWorker.ReportProgress(0);
对于(int i=1;i
立即返回,不等待工作人员完成
(因为它是异步的)

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

我想应该是这样的(未测试):

命名空间窗口窗体应用程序3
{
公共部分类Form1:Form
{
秒表秒表汇总=新秒表();
BackgroundWorker xBackgroundWorker=新的BackgroundWorker();
标签xLabel=新标签();
公共表格1()
{
初始化组件();
xBackgroundWorker.WorkerReportsProgress=true;
xBackgroundWorker.DoWork+=xBackgroundWorker\u DoWork;
xBackgroundWorker.ProgressChanged+=xBackgroundWorker\u ProgressChanged;
xLabel.Text=“xxxxxxxxxxxx”;
启动AVY计算();
}
私有void xbackgroundWorker_DoWork(对象发送方,DoWorkEventArgs e)
{
秒表_Summary.Start();
xBackgroundWorker.ReportProgress(0);

对于(int i=1;iIsn这不是您可以通过简单地使用调试器和一些断点来解决的问题吗?@设置不重复,因为他已经正确地使用了在UI线程上调用的
ProgressChanged
事件。我尝试将断点放在“xbackgroundWorker\u ProgressChanged”上发现它只在那里出现了一次。xLabel.Update();就在xLabel.Text=“随便什么”之后;您运行worker只是为了引发进度更改事件吗?并在UI线程上执行工作吗?这不是您可以通过简单地使用调试器和一些断点来解决的问题吗?@设置不重复,因为他已经正确地使用了在UI线程上调用的
ProgressChanged
事件。我尝试了o将断点放在“xbackgroundWorker_ProgressChanged”上,发现它只在xLabel.Text=“随便什么”之后出现一次。xLabel.Update();您运行worker只是为了引发进度更改事件吗?并在UI线程上执行工作?我如何修复它。我的过程顺序正确,对吗?启动秒表,然后运行workerasync,接下来是我的计算,最后是停止stopwatch@Vincent请。基本上,将您的代码放入
DoWork
,无需循环正如Uwe所说,长时间运行的计算代码应该放在DoWork中,不需要“无限”循环。此外,秒表可以在DoWork中启动。我已经添加了我认为应该是什么样子的代码(未测试)我怎么能解决这个问题。我的程序顺序正确,对吗?先启动秒表,然后启动runworkerasync,接下来是我的计算,最后停止stopwatch@Vincent请。基本上,把你的代码放到DoWork中,不需要循环或其他任何东西。正如Uwe所说,长时间运行的计算代码应该放到DoWork中,一个“无限”循环不是必需的。此外,秒表可以在DOWORK内部启动。我已经添加了我认为应该是什么样子的代码(未测试)
xBackgroundWorker.RunWorkerAsync(); 
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();       
        }
    }
}