为什么progressBar在接近结束时达到100%并超过90%?

时间:2012-08-20 21:15:12

标签: c#

从大约90%我没有看到数字运行91,92,93 ....酒吧刚刚结束然后它在100%

这是结束操作的菜单点击事件:

private void beginConvertionToolStripMenuItem_Click(object sender, EventArgs e)
        {
                automatic_ = true;
                button4.Enabled = true;
                beginConvertionToolStripMenuItem.Enabled = false;
                backgroundWorker1.RunWorkerAsync();
        }

然后是DoWork事件:

private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
        {
            if (automatic_ == true)
            {
                int y;
                string[] newDest_files;
                System.Drawing.Image NewImages;
                string sf;
                string[] images;
                sf = @"C:\Fraps\Screenshots\";
                images = Directory.GetFiles(sf, "*.bmp");
                for (int x = 0; x < images.Length; x++)
                {
                    int procents = ((x + 1) * 100) / images.Length;
                    backgroundWorker1.ReportProgress(procents, images[x]);
                    if (cancel_ == true)
                    {
                        e.Cancel = true;
                        break;
                    }

                        NewImages = System.Drawing.Image.FromFile(images[x]);
                        batch_Resize(NewImages);
                        if (NewImages != null)
                        NewImages.Dispose();
                }
            }
            e.Result = "All files converted successfully";
        }

batch_Resize功能代码:

private void batch_Resize(Image sourceImage)
        {
            Bitmap newImage = new Bitmap(512, 512);
            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(sourceImage, new Rectangle(0, 0, newImage.Width, newImage.Height));
                i = i + 1;
                newImage.Save(@"d:\NewImages\" + i.ToString("D6") + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            if (newImage != null)
                newImage.Dispose();
        }

进度改变事件:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;


            listBox1.Items.Add( "Converting File: " + e.UserState.ToString());
            textBox1.Text = e.ProgressPercentage.ToString();
            label4.Text = e.ProgressPercentage.ToString() + "%";

        }

完成的活动:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
               MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                listBox1.Items.Add("Convertion Have been canceled");
                button4.Enabled = false;
            }
            if (cancel_ == true)
            {
            }
            else
            {
                button4.Enabled = false;
            }
        }

似乎从90%开始,酒吧的进展继续绿色,但最后是100%,而不是从90到100。

1 个答案:

答案 0 :(得分:0)

可能发生得太快了。尝试添加等待时间(用于测试目的),以便检查它是否正确显示。

不确定如何在C#上执行此操作,但在Java上它将是一个Thread.sleep()