如何使ProgressBar工作?

时间:2012-02-09 22:56:18

标签: c# visual-studio visual-studio-2010

我正在使用VS 2010制作Windows窗体应用程序。我在表单中添加了ProgressBar,并更改了其值。但是,ProgressBar不会提前或更改。我认为其中一个原因是因为这个过程太快了,但我做得很慢而且也没用。

以下是代码:

void pn_TenPercentProgressEvent(object sender, PrimeNumberEventArgs e)
{  
    probarPrimeNumber.Visible = true;  
    probarPrimeNumber.Value = (int)e.Percent * 100;  
    this.Refresh();  
}

这是事件发生的类:

public ulong[] Generate(int count)
    {
        ulong[] ulngaryResult = new ulong[count];
        switch (count)
        {
            case 0:
                {
                    throw new Exception("The Zero is NOT Permitted.");
                }
            case 1:
                {
                    ulngaryResult[0] = 2;
                    break;
                }
            case 2:
                {
                    ulngaryResult[1] = 3;
                    goto case 1;
                }
            default:
                {
                    int intIndex = 0;
                    double dblPercent = 0.1;
                    for (ulong i = 2; i < ulong.MaxValue; i++)
                    {
                        if (this.IsPrime(i))
                        {
                            ulngaryResult[intIndex] = i;
                            intIndex++;
                        }
                        if (intIndex == (int)(dblPercent * count))
                        {
                            //Raise TenPercentProgressEvent
                            if (TenPercentProgressEvent != null) //Optional Event
                            {
                                TenPercentProgressEvent((object)this, new PrimeNumberEventArgs(intIndex, count, dblPercent));
                            }
                            dblPercent += 0.1;

                        }
                        if (intIndex == count)
                        {
                            //Raise FinishedProgressEvent
                            if (FinishedProgressEvent != null) //Optional Event
                            {
                                FinishedProgressEvent((object)this, new PrimeNumberEventArgs(intIndex, count, dblPercent));
                            }

                            break;
                        }
                    }
                    break;
                }
        }
        return ulngaryResult;
    }

1 个答案:

答案 0 :(得分:5)

试试(int)(e.Percent * 100)。实际上,在乘以100轮到0或1之前转换为int - 然后一直转换为0%或100%,但从不在它们之间。