Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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#在单独的线程中填充进度条_C#_Winforms_For Loop_Progress Bar - Fatal编程技术网

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

C#在单独的线程中填充进度条,c#,winforms,for-loop,progress-bar,C#,Winforms,For Loop,Progress Bar,我有一个进度条,我想用一个单独的线程来填充它,因为主线程在循环中处于休眠状态几秒钟。我正在使用计时器,以便进度条在一定时间内填满 线程创建: private void PlayButton_Click(object sender, EventArgs e) { progressBar1.Value = 0; int playTime = getPlayTime(); int progressInterval

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

线程创建:

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

        }
private void fillBar(int progressInterval)
{
progressTimer=new System.Windows.Forms.Timer();
progressTimer.Tick+=新的事件处理程序(clockTick);
progressTimer.Interval=progressInterval;//需要以多快的速度添加每个完成百分比
progressTimer.Start();
}
public void clockTick(对象发送方,事件参数e)
{
如果(progressBar1.0值<100)
{
progressBar1.Value++;
}
其他的
{
progressTimer.Stop();
}
}

你做得不对。主线程负责更新用户界面。因此,如果你用计算阻塞它,它将无法绘制进度条。将您的计算代码移动到另一个线程中,它应该可以正常工作。

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

winforms?wpf?银灯?(etc)@Muad'Dib从
计时器
类型和
单击
处理程序签名判断,这是WinForms。使用BackGroundWorker,这就是它的用途。我使用WinForms。我将研究BackGroundWorker,thanksI将计算代码移动到一个新线程中,就像您建议的那样,并在主线程中调用fillbar(),它成功了,非常感谢!出于好奇,我尝试为fillbar()创建一个新线程(如问题中所述),但没有成功,有什么想法可以解释为什么?可能是因为eventHandler没有添加到新线程?再次感谢:)