Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将进度条与进程同步_C#_Console Application - Fatal编程技术网

C# 如何将进度条与进程同步

C# 如何将进度条与进程同步,c#,console-application,C#,Console Application,我目前正在创建一个在控制台上工作的文件复制工具。其中有3个基本类,第一个是程序本身,它包含源和目标,如下所示: class Program { static void Main(string[] args) { Console.WriteLine("Source:"); string path = Console.ReadLine(); Console.WriteLine("tar

我目前正在创建一个在控制台上工作的文件复制工具。其中有3个基本类,第一个是程序本身,它包含源和目标,如下所示:

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Source:");
            string path = Console.ReadLine();

            Console.WriteLine("target:");

            string target = Console.ReadLine();


            Copy newCopy = new Copy();
            newCopy.CopyFunction(path, target);

            Console.ReadLine();
        }
    }
class Copy

    {
        public void CopyFunction(string source, string destination)
        {
            string sourceFile = source;
            string destinationFile = destination;

            File.Copy(sourceFile, destinationFile);

            Console.Write("Files are being copied... ");
            using (var progress = new ProgressBar())
            {
                for (int i = 0; i <= 100; i++)
                {
                    progress.Report((double)i / 100);
                    Thread.Sleep(20);
                }
            }

            Console.WriteLine("File Copied");         

        }

    }
第二类是Copy.CS,如下所示:

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Source:");
            string path = Console.ReadLine();

            Console.WriteLine("target:");

            string target = Console.ReadLine();


            Copy newCopy = new Copy();
            newCopy.CopyFunction(path, target);

            Console.ReadLine();
        }
    }
class Copy

    {
        public void CopyFunction(string source, string destination)
        {
            string sourceFile = source;
            string destinationFile = destination;

            File.Copy(sourceFile, destinationFile);

            Console.Write("Files are being copied... ");
            using (var progress = new ProgressBar())
            {
                for (int i = 0; i <= 100; i++)
                {
                    progress.Report((double)i / 100);
                    Thread.Sleep(20);
                }
            }

            Console.WriteLine("File Copied");         

        }

    }
类副本
{
public void CopyFunction(字符串源、字符串目标)
{
字符串sourceFile=source;
字符串destinationFile=目的地;
File.Copy(sourceFile,destinationFile);
写入(“正在复制文件…”);
使用(var progress=newprogressbar())
{

对于(int i=0;i要实现您想要做的事情,您需要在复制文件时更新进度条。一种方法是按块复制文件,并在复制每个块时报告进度。我修改了您的
CopyFunction
来实现这一点。尽情享受吧

class Copy

{
    public void CopyFunction(string sourcePath, string destinationPath)
    {
        byte[] buffer = new byte[1024 * 10]; // 10K buffer, you can change to larger size.

        using (var progress = new ProgressBar())
        using (FileStream source = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
        {
            long fileLength = source.Length;
            using (FileStream dest = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
            {
                long totalBytes = 0;
                int currentBlockSize = 0;

                while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    totalBytes += currentBlockSize;
                    dest.Write(buffer, 0, currentBlockSize);
                    progress.Report((double)totalBytes / fileLength);

                }
                progress.Report((double)1.0);
            }

            //File.Copy(sourceFile, destinationFile);

            //Console.Write("Files are being copied... ");
            //using (var progress = new ProgressBar())
            //{
            //    for (int i = 0; i <= 100; i++)
            //    {
            //        progress.Report((double)i / 100);
            //        Thread.Sleep(20);
            //    }
            //}

            Console.WriteLine("File Copied");

        }
    }
}
类副本
{
public void CopyFunction(字符串源路径、字符串目标路径)
{
byte[]buffer=新字节[1024*10];//10K缓冲区,您可以更改为更大的大小。
使用(var progress=newprogressbar())
使用(FileStream source=newfilestream(sourcePath,FileMode.Open,FileAccess.Read))
{
long fileLength=source.Length;
使用(FileStream dest=newfilestream(destinationPath,FileMode.Create,FileAccess.Write))
{
长totalBytes=0;
int currentBlockSize=0;
而((currentBlockSize=source.Read(buffer,0,buffer.Length))>0)
{
totalBytes+=currentBlockSize;
目的写入(缓冲区,0,currentBlockSize);
进度报告((两倍)总字节数/文件长度);
}
进度报告((双)1.0);
}
//File.Copy(sourceFile,destinationFile);
//写入(“正在复制文件…”);
//使用(var progress=newprogressbar())
//{

//为了(int i=0;顺便问一下,复制文件的函数会被视为递归算法吗?我试图理解这个概念。检查重复机制。它是同一个方法名,还是一种循环?在这种情况下,你可以看到有一个“while”循环,所以它不是递归。如果它自己调用,它会反复出现使用收缩的数据集(因此是递归)。数据必须变得“更小”,以便最终达到足够“小”的大小,从而停止重复并给出直接结果。(我引用了small,因为它可能更大,但更接近退出条件)@OriNachum那么将Robert建议的复制函数转换为递归算法可能/很难吗?可能是-从技术上讲,任何循环都可以是递归,但为什么?它不会给你任何优势。这种操作本质上是线性的(你不想并行复制一个文件-一次复制一个随机块),因此递归没有任何用途。你应该知道,如果可能的话,递归是要避免的,因为它们往往会消耗大量内存。@OriNachum我被赋予了一项任务,为复制文件制作一个递归算法,以确保我能证明我对它的理解。我不认为这需要递归,但我正在尝试只是因为有人要求我这么做,我才这么做。