我们有一些向控制台输出大量彩色文本的方法。缓存系统。控制台输出
它实际上是一个通过循环访问数据的菜单。
因为我们一个一个地写着项目(一行含有不同颜色的多个项目),所以这个过程在低端和中端机器上相当慢。在这些机器上,你可以看到每一行都被逐一写入,这对最终用户来说是相当令人沮丧的。
这个菜单必须显示很多次,所以我想知道我是否不能只是“缓存”这个输出。如果没有彩色文本,我只需将输出存储在一个字符串中并一次写入,但我不想丢失颜色信息。
有什么建议吗?
更新:只给输出是如何密集的一个想法: 更新2:更新马虎的代码,但还是要以某种方式缓冲输出。 MoveBufferArea被证明是一个部分解决方案(它产生不必要的滚动条)
DefaultBanner();
WriteLine("Available tests:\n", ConsoleColor.White);
var methodNames = methods.Select(m => ((TestAttribute)m.GetCustomAttributes(false)[0]).Name).ToArray();
int vertical = 0;
for (int i = 1; i <= methods.Length; i++)
{
if (i > methods.Length/2)
{
Console.SetCursorPosition(40, 4 + vertical++);
}
Write("(");
Write(i, ConsoleColor.Yellow);
WriteLine(") {0:00}", methodNames[i - 1]);
}
Write("\n(");
Write(items.Count + 1, ConsoleColor.Yellow);
Write(") Set address | (");
Write(items.Count + 2, ConsoleColor.Yellow);
Write(") View Log | (");
Write(items.Count + 3, ConsoleColor.Yellow);
Write(") Open Log directory \n(");
Write(items.Count + 4, ConsoleColor.Yellow);
Write(") Open configuration | (");
Write(items.Count + 5, ConsoleColor.Yellow);
Write(") View current configuration | (");
Write(items.Count + 6, ConsoleColor.Yellow);
WriteLine(") Quit");
Write("\nYour selection: ");
int command = 0;
while (!ConsoleReader<int>.TryReadValueInRange(1, items.Count + 6,out command));
return command;
write方法是简单地封装了System.Console类的一些行为 辅助方法,所以我们也不会保持设定的颜色。
但即使我存储这个,我仍然不得不在每次写入控制台时切换颜色。构建这个字符串的过程并不占用太多时间。这是对Console.Write,Console的众多调用。WriteLine和Console.SetCursorPosition和Console.Foreground =一直占用的调用。 (PS:在你没有注意到的快速机器上) – TimothyP 2009-11-23 07:56:20