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

2 回答 2

0

使用 AnimateProgBar(100),最终将创建一个 1 毫秒的间隔。

timerProgress.Interval = 毫秒;//不要除以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;
    }
}
于 2013-01-29T03:55:53.840 回答
0

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

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

于 2013-01-29T03:57:35.923 回答