【发布时间】:2023-03-29 13:24:01
【问题描述】:
我目前正在创建一个在控制台上工作的文件复制工具。其中存在 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();
}
}
第二类是Copy.CS,如下:
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");
}
}
对于最后的类,我实现了@DanielWolf提供的ProgressBar.cs类
https://gist.github.com/DanielSWolf/0ab6a96899cc5377bf54
我目前面临的问题是文件复制功能有效,进度条也有效,但它们是分开工作的。例如,控制台在处理正在发生的事情时会在空白屏幕上停留一段时间,然后在完成后显示进度条的快速动画。
我想知道是否可以将进度条与复制过程同步,以便在复制过程中以相似的速度移动?
【问题讨论】: