S
S
sputnickk2018-12-07 14:02:07
.NET
sputnickk, 2018-12-07 14:02:07

How to organize the process when copying?

Hello!
I copy an archive from a network resource. How to reflect the process?
First I used this method:

...
   int i = 0;
            FileStream fin = new FileStream(source, FileMode.Open);
            FileStream fout = new FileStream(destatntion, FileMode.Create);
            progressBarCopy1.Maximum = Convert.ToInt32(fin.Length);
            do
            {
                i = fin.ReadByte();
                if (i != -1)
                    fout.WriteByte((byte)i);

                //progressBarCopy1.Value = progressBarCopy1.Maximum - 1;
                progressBarCopy1.PerformStep();

            } while (i != -1);

            fin.Close();
            fout.Close();

            return i;
....

progressBarCopy1 is displayed, everything is OK, but for some reason it is copied from the network folder very slowly, with sagging and sometimes even broken files arrive.
I copy it like this:
int array_length = (int)Math.Pow(2, 19);
            byte[] dataArray = new byte[array_length];


            using (FileStream fsread = new FileStream
            (source, FileMode.Open, FileAccess.Read, FileShare.None, array_length))
            {
                progressBarCopy1.Maximum = Convert.ToInt32(fsread.Length);

                using (BinaryReader bwread = new BinaryReader(fsread))
                {
                    using (FileStream fswrite = new FileStream
                    (destination, FileMode.Create, FileAccess.Write, FileShare.None, array_length))
                    {
                        using (BinaryWriter bwwrite = new BinaryWriter(fswrite))
                        {
                            for (; ; )
                            {
                                progressBarCopy1.PerformStep();

                                int read = bwread.Read(dataArray, 0, array_length);
                                if (0 == read)
                                    break;
                                bwwrite.Write(dataArray, 0, read);
                            }
                        }
                    }
                }
            }

everything is copied very quickly, but the progress bar does not go
winforms Framework 3.5 , therefore asynchronous methods are not available

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lam0x86, 2018-12-07
@lam0x86

First, it's bad practice to perform long operations (greater than 100 milliseconds) on the UI thread. The application will hang until the file is copied. To avoid this, all disk operations must be asynchronous (FileStream.BeginRead/EndRead can be used instead of async/await).
Secondly, your buffer is too big. 2^19 is 512 kilobytes. You may be copying smaller files, so the entire copy is done in a single read operation. Try a buffer, say 16kb.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question