【问题标题】:C# Filling progress bar in separate threadC#在单独的线程中填充进度条
【发布时间】:2012-01-28 15:25:22
【问题描述】:

我有一个进度条,想用一个单独的线程来填充它,因为主线程在一个循环中休眠了几秒钟。我正在使用计时器,以便进度条在一定时间内填满。

线程创建:

private void PlayButton_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            int playTime = getPlayTime();
            int progressInterval = playTime / 100;
            Thread progressThread = new Thread(barfiller=>fillBar(progressInterval));
            progressThread.Start();

            //Loops through the collection and plays each note one after the other
            foreach (MusicNote music in this.staff.Notes)
            {
                music.Play(music.Dur);
                Thread.Sleep(music.getInterval(music.Dur));
            }
            progressThread.Abort();
        }

按原样,进度条没有任何反应,但是如果我在主线程中调用 fillbar(),它会工作,但它会在 for 循环完成后填充,而不是在 for 循环之前/期间填充,即使我调用了 fillbar( ) 在循环之前。

线程方法:

private void fillBar(int progressInterval)
        {
            progressTimer = new System.Windows.Forms.Timer();
            progressTimer.Tick += new EventHandler(clockTick);
            progressTimer.Interval = progressInterval; //How fast every percentage point of completion needs to be added
            progressTimer.Start();

        }

        public void clockTick(object sender, EventArgs e)
        {
            if (progressBar1.Value < 100)
            {
                progressBar1.Value++;
            }
            else
            {
                progressTimer.Stop();
            }

        }

【问题讨论】:

  • winforms? wpf?银光? (等)
  • @Muad'Dib 从Timer 类型和Click 处理程序签名来看,它是WinForms。
  • 使用 BackGroundWorker,这就是它的用途。
  • 我正在使用 winforms。我会调查 BackGroundWorker,谢谢

标签: c# winforms for-loop progress-bar


【解决方案1】:

你做错了。主线程负责更新用户界面。因此,如果您用计算阻止它,它将无法绘制进度条。将您的计算代码移到另一个线程中,应该没问题。

【讨论】:

  • 我按照您的建议将计算代码移到了一个新线程中,并在主线程中调用了 fillbar() 并且它有效,非常感谢!出于好奇,我尝试为 fillbar() 创建一个新线程(就像问题中一样),但这没有用,有什么想法吗?也许是因为 eventHandler 也没有添加到新线程中?再次感谢:)
【解决方案2】:

始终是管理用户界面的主线程。为此目的使用backgroundworker。 在 backgroundworker 中启用进度功能将 WorkerReportProgress(property) 设置为 true 和 如果需要,设置 WorkerSupportCancellation 以停止后台工作人员。

  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
         // also use sender as backgroundworker
        int i = 0;
        foreach (MusicNote music in this.staff.Notes)
        {
            if(backgroundWorker1.CancellationPending) return;
             music.Play(music.Dur);
            Thread.Sleep(music.getInterval(music.Dur));

            int p =  (int) (i*100/ staff.Notes.Count); /*Count or Length */
            backgroundWorker1.ReportProgress(p);
            i++;
        }
        backgroundWorker1.ReportProgress(100);
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多