简体   繁体   English

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

[英]C# Filling progress bar in separate thread

I have a progress bar and want to fill it in using a separate thread, because the main thread is put to sleep for a few seconds in a loop. 我有一个进度条,想用一个单独的线程来填充它,因为主线程循环进入休眠状态几秒钟。 I'm using a timer so that the progress bar fills up over a certain amount of time. 我使用计时器,以便进度条在一定时间内充满。

Thread creation: 线程创建:

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

As is, nothing happens to the progress bar, if however I call fillbar() within the main thread, it works BUT it fills after the for loop is complete and not before/during the for loop even though I call fillbar() before the loop. 实际上,进度条没有任何反应,但是,如果我在主线程中调用fillbar(),则它可以工作,但它会在for循环完成后填充,而不是在for循环之前/期间填充,即使我在for循环之前调用fillbar()环。

Thread methods: 线程方法:

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

        }

You're doing it the wrong way. 你这样做是错误的。 The main thread is reponsible of updating the user interface. 主线程负责更新用户界面。 So if you're blocking it with your calculations, it won't be able to draw the progress bar. 因此,如果您用计算来阻止它,它将无法绘制进度条。 Move your computing code in another thread and it should be fine. 将计算代码移到另一个线程中,应该没问题。

always the main thread for manage user interface. 始终是管理用户界面的主线程。 use backgroundworker for this purpose. 为此使用backgroundworker to enable progress feature in backgroundworker set WorkerReportProgress(property) to true and set WorkerSupportCancellation for stopping backgroundworker if needed. 要启用backgroundworker中的进度功能,请将WorkerReportProgress(property)设置为true,并设置WorkerSupportCancellation以便在需要时停止backgroundworker。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM