我有一个显示估计剩余时间的程序,但在执行此操作时还会打印其他内容。我想打印其他部分而不影响计时器的位置。
这是我的示例输出:
Processing the files into smaller chunks ... 90% 00:00:01 left. Finished processing files Now Comparing ... AffiliatePairoffSnapshot 10% 00:00:08 left. Total: 0 AllocationSnapshot 23% 00:00:07 left. .trans.links[0].type.TransLinkType 102 .trans.links[0].fromTransactionRef.versionHandle.instanceHandle.id 102 Total: 204 TradeSnapshot Total: 0 TransferSnapshot 86% 00:00:01 left. .trans.links[0].size.amount 5 .trans.links[0].fromTransactionRef.versionHandle.instanceHandle.id 5 .trans.activityContextRef[0].versionHandle.instanceHandle.id 3708 Total: 3718 Total Number Different Fields Across All ObjectTypes: 3922 The Generated xml files can be found at: c:\Users\grover\packer\XML
我想打印在自己的行上留下的时间,并且没有受影响的其他打印有没有办法做到这一点?
答案 0 :(得分:0)
如果真的需要这样做,你可以overwrite what was already written to the console,但这会产生相当糟糕的用户体验,除非它不经常更新
答案 1 :(得分:0)
让打印时间的代码执行以下操作:
以下是该概念的简单演示:
private static void Main()
{
PrintTimes();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Some text is being printed");
Thread.Sleep(1000);
}
Console.WriteLine();
Console.WriteLine("press any key to exit . . .");
Console.ReadKey();
}
private static async void PrintTimes()
{
int line = Console.CursorTop;
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
int previousLine = Console.CursorTop;
int previousChar = Console.CursorLeft;
Console.CursorTop = line;
Console.CursorLeft = 0;
Console.WriteLine("{0}% left ", 100 - i * 10);
Console.CursorTop = previousLine;
Console.CursorLeft = previousChar;
await Task.Delay(1000);
}
}
请注意,在此演示中,PrintTimes
中完成的单词不是原子的,因此仍然可以在其中一些操作之间注入另一个控制台写入。如果您最终经常写入控制台,则需要确保同步所有控制台写入。