コード例 #1
0
        public void CopyFileEx()
        {
            var sw = Stopwatch.StartNew();

            progressBar1.Value = 0;

            CopyFileCallbackAction myCallback(FileInfo source, FileInfo destination, object state, long totalFileSize, long totalBytesTransferred)
            {
                double dProgress = (totalBytesTransferred / (double)totalFileSize) * 100.0;

                progressBar1.Value = (int)dProgress;

                return(CopyFileCallbackAction.Continue);
            }

            FileRoutines.CopyFile(textBox1.Text, textBox2.Text, myCallback);

            sw.Stop();

            if (textBox3.Text.Length > 0)
            {
                textBox3.AppendText("\r\n");
            }

            textBox3.AppendText($"Copy CopyFileEx No Thread: " + sw.ElapsedMilliseconds);
        }
コード例 #2
0
        public void CopyFileExThread()
        {
            button1.Enabled = false;
            var sw = Stopwatch.StartNew();

            progressBar1.Value = 0;

            var sourceText      = textBox1.Text;
            var destinationText = textBox2.Text;

            var thr = new Thread(() =>
            {
                CopyFileCallbackAction myCallback(FileInfo source, FileInfo destination, object state, long totalFileSize, long totalBytesTransferred)
                {
                    double dProgress = (totalBytesTransferred / (double)totalFileSize) * 100.0;

                    this.Invoke((MethodInvoker) delegate { progressBar1.Value = (int)dProgress; });

                    return(CopyFileCallbackAction.Continue);
                }

                FileRoutines.CopyFile(sourceText, destinationText, myCallback);
            });

            thr.SetApartmentState(ApartmentState.MTA);
            thr.Priority = ThreadPriority.Highest;
            thr.Start();

            while (thr.IsAlive)
            {
                Application.DoEvents();
                Thread.Sleep(1);
            }


            button1.Enabled = true;



            sw.Stop();

            if (textBox3.Text.Length > 0)
            {
                textBox3.AppendText("\r\n");
            }

            textBox3.AppendText($"Copy CopyFileEx Thread: " + sw.ElapsedMilliseconds);
        }