C#在单独的线程中填写进度条

时间:2011-12-27 16:46:54

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

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

线程创建:

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

        }

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:1)

始终是管理用户界面的主线程。为此目的使用backgroundworker。 在backgroundworker中将进度功能设置为WorkerReportProgress(属性)为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;
    }