我的后台工作者报告超过100%

 保佑麻木_711 发布于 2023-02-13 17:53

我正在编写一个复制文件的程序.我有正确的文件复制,进度条更新,但我收到一个错误,指出e.ProgressPercentage是101. bgWorker_ProgressChanged事件处理程序的代码是:

private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // We will increase the progress bar when work progress is reported.
                    pbCopyProgress.Maximum = 100;
        pbCopyProgress.Value = e.ProgressPercentage;        
    }

以下是bgWorker_DoWork事件处理程序的代码:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Gets the size of the file in bytes.
        Int64 iSize = strInputFile.Length;

        // Keeps track of the total bytes downloaded so we can update the progress bar.
        Int64 iRunningByteTotal = 0;
            // Open the input file for reading.
        using (FileStream InputFile = new FileStream(strInputFile, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            // Using the FileStream object, we can write the output file.
            using (FileStream OutputFile = new FileStream(strOutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                // Loop the stream and get the file into the byte buffer.
                int iByteSize = 0;
                byte[] byteBuffer = new byte[iSize];

                while ((iByteSize = InputFile.Read(byteBuffer, 0, byteBuffer.Length)) > 0)                    
                {
                    // Calculate the progress out of a base "100."
                    double dIndex;
                    double dTotal;
                    double dProgressPercentage;
                    // Write the bytes to the file system at the file path specified.                        
                    dIndex = (double)(iRunningByteTotal);
                    dTotal = (double)byteBuffer.Length;
                    dProgressPercentage = (dIndex / dTotal);
                    OutputFile.Write(byteBuffer, 0, iByteSize);
                    iRunningByteTotal += iByteSize;
                    intProgress = Convert.ToUInt16(dProgressPercentage);
                    // Update the progress bar.
                    bgWorker.ReportProgress(intProgress);

                }                    
                // Close the output file.
                OutputFile.Close();
            }
            // Close the input file.
            InputFile.Close();
        }
    }

正如我所说,进度条正在更新,但我收到错误,因为它似乎在文件达到100%后继续复制.如果我在bgWorker.ReportProgress(intProgress)行之后立即放入MessageBox.Show(Convert.ToString(intProgress)),对话框将弹出101文本.任何帮助将不胜感激.

1 个回答
  • 您将运行总计除以块缓冲区的长度,而不是整个流,这意味着结果基本上是无限制的.你也没有乘以100,但这个问题被比率增长大于1的事实掩盖了.

    但是你让它看起来非常困难 - 你想要的代码就是:

     bgWorker.ReportProgress((int)(100 * runningByteTotal / fileLength))
    

    你应该在循环开始之前设置fileLength(它需要是文件的长度,而不是文件名,正如@azyberezovsky在他的回答中指出的那样).

    您可以使用简单的整数运算来进行此计算,而不是需要浮点类型,只要在除数之前乘以100即可.

    作为一个风格点,你不需要在变量名前面的所有'我和's' - 这不被认为是好的C#风格.变量通常也不是以大写字母开头的 - 如果没有别的话,会混淆SO代码语法高亮显示...

    2023-02-13 17:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有