How can I make the progress bar update fast enough?

后端 未结 9 1739
礼貌的吻别
礼貌的吻别 2021-02-08 20:40

I\'m using a progress bar to show the user how far along the process is. It has 17 steps, and it can take anywhere from ~5 seconds to two or three minutes depending on the weath

9条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-08 20:58

    The reason for this whole mess is the interpolating animation effect introduced by Vista and W7. It has aboslutely nothing to do with thread blocking issues. Calling setProgress() or setting the Value property driectly, triggers an animation effect to occur, which I will explain how to cheat:

    I came up with a hack of setting the maximum according to a fixed value. The maximum property does not trigger the effect, thus you get to freely move the progress around with instant response.

    Remember that the actual shown progress is given by: ProgressBar.Value / ProgressBar.Maximum. With this in mind, the example below will move the progress from 0 to 100, repensented by i:

    ProgressBar works like this:  
    progress = value / maximum
    
    therefore:
    maximum = value / progress
    

    I added some scaling factors needed, should be self explanatory:

    progressBar1.Maximum *= 100;
    progressBar1.Value = progressBar1.Maximum / 100;
    for (int i = 1; i < 100; i++)
    {
        progressBar1.Maximum = (int)((double)progressBar1.Value / (double)(i + 1) * 100);
        Thread.Sleep(20);
    }
    

提交回复
热议问题