首页 文章

进度条运行的时间少于定义的时间

提问于
浏览
0

我创建了一个获得毫秒数的函数,然后运行进度条,但结果是进度条的运行时间少于定义的时间 .

this.timerProgress.Tick += new System.EventHandler(this.timerProgress_Tick);

public void AnimateProgBar(int milliSeconds)
{
    if (!timerProgress.Enabled)
    {
        this.Invoke((MethodInvoker)delegate { pbStatus.Value = 0; });
        timerProgress.Interval = milliSeconds / 100;
        timerProgress.Enabled = true;
    }
}

private void timerProgress_Tick(object sender, EventArgs e)
{
    if (pbStatus.Value < 100)
    {
        pbStatus.Value += 1;
        pbStatus.Refresh();
    }
    else
    {
        timerProgress.Enabled = false;
    }
}

2 回答

  • 0

    使用AnimateProgBar(100),最终将创建1毫秒的Interval .

    timerProgress.Interval = milliSeconds; //不要除以100

    this.timerProgress.Tick += new System.EventHandler(this.timerProgress_Tick);
    
    public void AnimateProgBar(int milliSeconds)
    {
        if (!timerProgress.Enabled)
        {
            this.Invoke((MethodInvoker)delegate { pbStatus.Value = 0; });
            timerProgress.Interval = milliSeconds; //do not divide by 100
            timerProgress.Enabled = true;
        }
    }
    
    private void timerProgress_Tick(object sender, EventArgs e)
    {
        if (pbStatus.Value < 100)
        {
            pbStatus.Value += 1;
            pbStatus.Refresh();
        }
        else
        {
            timerProgress.Enabled = false;
        }
    }
    
  • 0

    AnimateProgBar(1000) 的调用将导致以下计算: 1000 / 100 . 这等于 10 .

    Timer 间隔已经是毫秒 . 所以你有效地将间隔设置为 10ms .

相关问题