我們有一些向控制檯輸出大量彩色文本的方法。緩存系統。控制檯輸出
它實際上是一個通過循環訪問數據的菜單。
因爲我們一個一個地寫着項目(一行含有不同顏色的多個項目),所以這個過程在低端和中端機器上相當慢。在這些機器上,你可以看到每一行都被逐一寫入,這對最終用戶來說是相當令人沮喪的。
這個菜單必須顯示很多次,所以我想知道我是否不能只是「緩存」這個輸出。如果沒有彩色文本,我只需將輸出存儲在一個字符串中並一次寫入,但我不想丟失顏色信息。
有什麼建議嗎?
更新:只給輸出是如何密集的一個想法: 更新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