Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Progress Bar makes copying painfully slow, a little help please?

Progress Bar makes copying painfully slow, a little help please?

Scheduled Pinned Locked Moved C#
designhelpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kiasta
    wrote on last edited by
    #1

    I've implemented a progress bar to my application but it makes the file copy process painfully slow whereas without the progress bar the copy is very fast. I know without the progress bar it only takes a few seconds to copy a 10 megabyte file but with the progress bar it takes more than a minute. What am I doing wrong?

    namespace Compression_Util
    {
    class DropBox
    {
    private object sender;
    private EventArgs e;
    private DragEventArgs de;
    private Design zip;
    private string hover;
    private string[] files;
    private string filetype;

        public DropBox(object sender, DragEventArgs de, Design zip, string hover)
        {
            this.sender = sender;
            this.de = de;
            this.zip = zip;
            this.hover = hover;
            this.Hover();
        }
    
        public DropBox(object sender, EventArgs e, Design zip, string hover)
        {
            this.sender = sender;
            this.e = e;
            this.zip = zip;
            this.hover = hover;
            this.Hover();
        }
    
        private void InitializeProgressBar(int fileSize)
        {
            zip.DropProgress.Visible = true;
            zip.DropProgress.Minimum = sizeof(Byte);
            zip.DropProgress.Step = sizeof(Byte);
            zip.DropProgress.Maximum = fileSize;
        }
    
        private void DeInitializeProgressBar()
        {
            zip.DropProgress.Invalidate();
            zip.DropProgress.Visible = false;
        }
    
        private void IncrementProgressBar()
        {
            zip.DropProgress.PerformStep();
        }
    
        private void CreateFile(string read, string write)
        {
            try
            {
                FileStream fileStreamReader = new FileStream(read, FileMode.Open, FileAccess.Read);
                FileStream fileStreamWriter = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                BinaryReader binaryReader = new BinaryReader(fileStreamReader);
                BinaryWriter binaryWriter = new BinaryWriter(fileStreamWriter);
                int position = 0;
                int length = (int)binaryReader.BaseStream.Length;
                InitializeProgressBar(length);
                while (position < length)
                {
                    Byte line = binaryReader.ReadByte();
                    binaryWriter.Write(line);
                    position += sizeof(Byte);
                    Increm
    
    R R OriginalGriffO 3 Replies Last reply
    0
    • K kiasta

      I've implemented a progress bar to my application but it makes the file copy process painfully slow whereas without the progress bar the copy is very fast. I know without the progress bar it only takes a few seconds to copy a 10 megabyte file but with the progress bar it takes more than a minute. What am I doing wrong?

      namespace Compression_Util
      {
      class DropBox
      {
      private object sender;
      private EventArgs e;
      private DragEventArgs de;
      private Design zip;
      private string hover;
      private string[] files;
      private string filetype;

          public DropBox(object sender, DragEventArgs de, Design zip, string hover)
          {
              this.sender = sender;
              this.de = de;
              this.zip = zip;
              this.hover = hover;
              this.Hover();
          }
      
          public DropBox(object sender, EventArgs e, Design zip, string hover)
          {
              this.sender = sender;
              this.e = e;
              this.zip = zip;
              this.hover = hover;
              this.Hover();
          }
      
          private void InitializeProgressBar(int fileSize)
          {
              zip.DropProgress.Visible = true;
              zip.DropProgress.Minimum = sizeof(Byte);
              zip.DropProgress.Step = sizeof(Byte);
              zip.DropProgress.Maximum = fileSize;
          }
      
          private void DeInitializeProgressBar()
          {
              zip.DropProgress.Invalidate();
              zip.DropProgress.Visible = false;
          }
      
          private void IncrementProgressBar()
          {
              zip.DropProgress.PerformStep();
          }
      
          private void CreateFile(string read, string write)
          {
              try
              {
                  FileStream fileStreamReader = new FileStream(read, FileMode.Open, FileAccess.Read);
                  FileStream fileStreamWriter = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                  BinaryReader binaryReader = new BinaryReader(fileStreamReader);
                  BinaryWriter binaryWriter = new BinaryWriter(fileStreamWriter);
                  int position = 0;
                  int length = (int)binaryReader.BaseStream.Length;
                  InitializeProgressBar(length);
                  while (position < length)
                  {
                      Byte line = binaryReader.ReadByte();
                      binaryWriter.Write(line);
                      position += sizeof(Byte);
                      Increm
      
      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      kiasta wrote:

      What am I doing wrong?

      You're performing UI operations during the copy.  What you should instead do is use a BackgroundWorker[^] to perform the file copy and publish UI updates. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      K 1 Reply Last reply
      0
      • K kiasta

        I've implemented a progress bar to my application but it makes the file copy process painfully slow whereas without the progress bar the copy is very fast. I know without the progress bar it only takes a few seconds to copy a 10 megabyte file but with the progress bar it takes more than a minute. What am I doing wrong?

        namespace Compression_Util
        {
        class DropBox
        {
        private object sender;
        private EventArgs e;
        private DragEventArgs de;
        private Design zip;
        private string hover;
        private string[] files;
        private string filetype;

            public DropBox(object sender, DragEventArgs de, Design zip, string hover)
            {
                this.sender = sender;
                this.de = de;
                this.zip = zip;
                this.hover = hover;
                this.Hover();
            }
        
            public DropBox(object sender, EventArgs e, Design zip, string hover)
            {
                this.sender = sender;
                this.e = e;
                this.zip = zip;
                this.hover = hover;
                this.Hover();
            }
        
            private void InitializeProgressBar(int fileSize)
            {
                zip.DropProgress.Visible = true;
                zip.DropProgress.Minimum = sizeof(Byte);
                zip.DropProgress.Step = sizeof(Byte);
                zip.DropProgress.Maximum = fileSize;
            }
        
            private void DeInitializeProgressBar()
            {
                zip.DropProgress.Invalidate();
                zip.DropProgress.Visible = false;
            }
        
            private void IncrementProgressBar()
            {
                zip.DropProgress.PerformStep();
            }
        
            private void CreateFile(string read, string write)
            {
                try
                {
                    FileStream fileStreamReader = new FileStream(read, FileMode.Open, FileAccess.Read);
                    FileStream fileStreamWriter = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                    BinaryReader binaryReader = new BinaryReader(fileStreamReader);
                    BinaryWriter binaryWriter = new BinaryWriter(fileStreamWriter);
                    int position = 0;
                    int length = (int)binaryReader.BaseStream.Length;
                    InitializeProgressBar(length);
                    while (position < length)
                    {
                        Byte line = binaryReader.ReadByte();
                        binaryWriter.Write(line);
                        position += sizeof(Byte);
                        Increm
        
        R Offline
        R Offline
        Roger500
        wrote on last edited by
        #3

        Not only are you copying the file in the UI, you are updating the progress bar every IO. I always calculate percent complete using the following formula: (number of bytes copied * 100) / size of file in bytes All numbers used in the calculation are long but the result is converted to int. Update the progress bar only when the percent changes.

        K 1 Reply Last reply
        0
        • K kiasta

          I've implemented a progress bar to my application but it makes the file copy process painfully slow whereas without the progress bar the copy is very fast. I know without the progress bar it only takes a few seconds to copy a 10 megabyte file but with the progress bar it takes more than a minute. What am I doing wrong?

          namespace Compression_Util
          {
          class DropBox
          {
          private object sender;
          private EventArgs e;
          private DragEventArgs de;
          private Design zip;
          private string hover;
          private string[] files;
          private string filetype;

              public DropBox(object sender, DragEventArgs de, Design zip, string hover)
              {
                  this.sender = sender;
                  this.de = de;
                  this.zip = zip;
                  this.hover = hover;
                  this.Hover();
              }
          
              public DropBox(object sender, EventArgs e, Design zip, string hover)
              {
                  this.sender = sender;
                  this.e = e;
                  this.zip = zip;
                  this.hover = hover;
                  this.Hover();
              }
          
              private void InitializeProgressBar(int fileSize)
              {
                  zip.DropProgress.Visible = true;
                  zip.DropProgress.Minimum = sizeof(Byte);
                  zip.DropProgress.Step = sizeof(Byte);
                  zip.DropProgress.Maximum = fileSize;
              }
          
              private void DeInitializeProgressBar()
              {
                  zip.DropProgress.Invalidate();
                  zip.DropProgress.Visible = false;
              }
          
              private void IncrementProgressBar()
              {
                  zip.DropProgress.PerformStep();
              }
          
              private void CreateFile(string read, string write)
              {
                  try
                  {
                      FileStream fileStreamReader = new FileStream(read, FileMode.Open, FileAccess.Read);
                      FileStream fileStreamWriter = new FileStream(write, FileMode.Create, FileAccess.ReadWrite);
                      BinaryReader binaryReader = new BinaryReader(fileStreamReader);
                      BinaryWriter binaryWriter = new BinaryWriter(fileStreamWriter);
                      int position = 0;
                      int length = (int)binaryReader.BaseStream.Length;
                      InitializeProgressBar(length);
                      while (position < length)
                      {
                          Byte line = binaryReader.ReadByte();
                          binaryWriter.Write(line);
                          position += sizeof(Byte);
                          Increm
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          To add to what the others say, you are also doing this byte by byte! Buffer it: allocate a buffer of say 1/2 meg and fill it. Write it, update progress. Fill it, write it, update progress. But I do it in a background worker like this:

              /// <summary>
              /// Do the actual file move.
              /// </summary>
              /// <param name="src"></param>
              /// <param name="dst"></param>
              /// <param name="worker"></param>
              /// <param name="prMain"></param>
              private void MoveFile(string src, string dst, BackgroundWorker worker = null, ProgressReport prMain = null)
                  {
                  if (src != dst)
                      {
                      // Copy the file itself.
                      int iSrc = src.IndexOf(':');
                      int iDst = dst.IndexOf(':');
                      FileInfo fiSrc = new FileInfo(src);
                      if (fiSrc.Length < blockSize || (iSrc > 0 && iDst > 0 && iSrc == iDst && src.Substring(0, iSrc) == dst.Substring(0, iDst)))
                          {
                          // On same drive or trivial size - move it via the file system
                          if (doCopyOnly)
                              {
                              File.Copy(src, dst);
                              }
                          else
                              {
                              File.Move(src, dst);
                              }
                          }
                      else
                          {
                          // Needs to be moved "properly".
                          using (Stream sr = new FileStream(src, FileMode.Open))
                              {
                              using (Stream sw = new FileStream(dst, FileMode.Create))
                                  {
                                  long total = sr.Length;
                                  long bytes = 0;
                                  long cnt = total;
                                  int progress = 0;
                                  while (cnt > 0)
                                      {
                                      int n = sr.Read(transfer, 0, blockSize);
                                      sw.Write(transfer, 0, n);
                                      bytes += n;
                                      cnt -= n;
                                      int percent = (int)((bytes \* 100) / total);
                                      if (progress != percent)
                                          {
                                          // Report progress
          

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          K 1 Reply Last reply
          0
          • R Ravi Bhavnani

            kiasta wrote:

            What am I doing wrong?

            You're performing UI operations during the copy.  What you should instead do is use a BackgroundWorker[^] to perform the file copy and publish UI updates. /ravi

            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

            K Offline
            K Offline
            kiasta
            wrote on last edited by
            #5

            Thanks for the reply! I will definitely look into that.

            1 Reply Last reply
            0
            • R Roger500

              Not only are you copying the file in the UI, you are updating the progress bar every IO. I always calculate percent complete using the following formula: (number of bytes copied * 100) / size of file in bytes All numbers used in the calculation are long but the result is converted to int. Update the progress bar only when the percent changes.

              K Offline
              K Offline
              kiasta
              wrote on last edited by
              #6

              Hmm OK that makes some sense, but why would copying it inside the UI make any difference? Without the progress bar the files copy very fast, almost instantly. I don't really understand, I guess. Is the progress bar really that resource intensive?

              R 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                To add to what the others say, you are also doing this byte by byte! Buffer it: allocate a buffer of say 1/2 meg and fill it. Write it, update progress. Fill it, write it, update progress. But I do it in a background worker like this:

                    /// <summary>
                    /// Do the actual file move.
                    /// </summary>
                    /// <param name="src"></param>
                    /// <param name="dst"></param>
                    /// <param name="worker"></param>
                    /// <param name="prMain"></param>
                    private void MoveFile(string src, string dst, BackgroundWorker worker = null, ProgressReport prMain = null)
                        {
                        if (src != dst)
                            {
                            // Copy the file itself.
                            int iSrc = src.IndexOf(':');
                            int iDst = dst.IndexOf(':');
                            FileInfo fiSrc = new FileInfo(src);
                            if (fiSrc.Length < blockSize || (iSrc > 0 && iDst > 0 && iSrc == iDst && src.Substring(0, iSrc) == dst.Substring(0, iDst)))
                                {
                                // On same drive or trivial size - move it via the file system
                                if (doCopyOnly)
                                    {
                                    File.Copy(src, dst);
                                    }
                                else
                                    {
                                    File.Move(src, dst);
                                    }
                                }
                            else
                                {
                                // Needs to be moved "properly".
                                using (Stream sr = new FileStream(src, FileMode.Open))
                                    {
                                    using (Stream sw = new FileStream(dst, FileMode.Create))
                                        {
                                        long total = sr.Length;
                                        long bytes = 0;
                                        long cnt = total;
                                        int progress = 0;
                                        while (cnt > 0)
                                            {
                                            int n = sr.Read(transfer, 0, blockSize);
                                            sw.Write(transfer, 0, n);
                                            bytes += n;
                                            cnt -= n;
                                            int percent = (int)((bytes \* 100) / total);
                                            if (progress != percent)
                                                {
                                                // Report progress
                
                K Offline
                K Offline
                kiasta
                wrote on last edited by
                #7

                The main objective is to compress the files into a container, but I want to at least be able to copy files with some type of progress notification at the very least first before I start making an algorithm for compression. Thanks for the source I'll study it and try to make something work.

                1 Reply Last reply
                0
                • K kiasta

                  Hmm OK that makes some sense, but why would copying it inside the UI make any difference? Without the progress bar the files copy very fast, almost instantly. I don't really understand, I guess. Is the progress bar really that resource intensive?

                  R Offline
                  R Offline
                  Roger500
                  wrote on last edited by
                  #8

                  As you said, the progress bar slows down the copy. The progress bar is CPU intensive while the file copy is most likely IO bound. The progress bar slows the copy when the two operations share the same thread. Copying the file in a separate thread will allow more efficient use of multiple processors; both operations run almost independently. Also, try copying a file several gigabytes in size and watch your UI lock up. I tend to reuse code a lot. Code a process right the first time and use it many times.

                  K 1 Reply Last reply
                  0
                  • R Roger500

                    As you said, the progress bar slows down the copy. The progress bar is CPU intensive while the file copy is most likely IO bound. The progress bar slows the copy when the two operations share the same thread. Copying the file in a separate thread will allow more efficient use of multiple processors; both operations run almost independently. Also, try copying a file several gigabytes in size and watch your UI lock up. I tend to reuse code a lot. Code a process right the first time and use it many times.

                    K Offline
                    K Offline
                    kiasta
                    wrote on last edited by
                    #9

                    Ah OK I see now what you mean, thanks for the clarification!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups