Progressbar不报告进度

时间:2012-08-28 19:37:19

标签: c# winforms multithreading progress-bar

我可能在相同的代码中盯着太长时间,但我似乎无法让进度条在下面的代码中工作。有人有任何提示吗?

private void doBackgroundTIFConverter(object sender, DoWorkEventArgs e)
{
    string tifFileName = (string)e.Argument;
    bm = new Bitmap(tifFileName);
    int total = bm.GetFrameCount(FrameDimension.Page);
    document.Open();
    cb = writer.DirectContent;
    for (int k = 0; k < total; ++k)
    {
        backgroundTIFConverter.ReportProgress(k / total * 100);
        bm.SelectActiveFrame(FrameDimension.Page, k);
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, ImageFormat.Bmp);
        // scale the image to fit in the page
        img.ScalePercent(72f / img.DpiX * 100);
        img.SetAbsolutePosition(0, 0);
        cb.AddImage(img);
        document.NewPage();
    }
    bm.Dispose();
    document.Close();
}

private void progressBackgroundTIFConverter(object sender, ProgressChangedEventArgs e)
{
    splitProgressBar.Value = e.ProgressPercentage;
}

private void completedBackgroundTIFConverter(object sender, RunWorkerCompletedEventArgs e)
{
    splitButton.Enabled = true;
}


private void completedBackgroundTIFConverter(object sender, RunWorkerCompletedEventArgs e)
{
    splitButton.Enabled = true;
}

1 个答案:

答案 0 :(得分:9)

backgroundTIFConverter.ReportProgress(k / total * 100);

这是一个非常常见的C#陷阱,你将在这里获得整数除法。 k / total将始终为零。将表达式重新排序为:

backgroundTIFConverter.ReportProgress(k * 100 / total);

如果你想达到100%,那么从总数中减去1:k * 100 /(total-1)