簡體   English   中英

文件復制進度條未更新。 文件復制工作正常

[英]File copy progress bar not updating. File copy works fine

我有一個我正在編寫的應用程序來復制文件。 我有沒有任何問題的文件復制,但進度條由於某種原因沒有更新。 我正在使用后台工作者。 這是代碼:

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)
                    {
                        // Write the bytes to the file system at the file path specified.
                        OutputFile.Write(byteBuffer, 0, iByteSize);
                        iRunningByteTotal += iByteSize;

                        // Calculate the progress out of a base "100."
                        double dIndex = (double)(iRunningByteTotal);
                        double dTotal = (double)byteBuffer.Length;
                        double dProgressPercentage = (dIndex / dTotal);
                        int iProgressPercentage = (int)(dProgressPercentage * 100);
                        // Update the progress bar.
                       bgWorker.WorkerReportsProgress = true;
                        bgWorker.ReportProgress(iProgressPercentage);
                    }
                    // Close the output file.
                    OutputFile.Close();
                }
                // Close the input file.
                InputFile.Close();
            }
        }

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

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Disable the Copy button once the file has been copied.
            MessageBox.Show("The file: "+strOutputFile+" has been copied");
            btnCopy.Enabled = false;
        }

我發現我需要初始化我的后台工作程序事件處理程序,以解決我遇到的問題。 我只需要在表單加載事件中添加以下三行:

 bgWorker.DoWork += bgWorker_DoWork;
 bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
 bgWorker.ProgressChanged += bgWorker_ProgressChanged;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM