Caching System.Console output

We have some methods that output a lot of colored text to the Console.

This is actually a menu that is created by cyclic data collection.

Since we write an element by element (and one line contains many elements in different colors), this process is rather slow on low-performance and medium-sized machines. On these machines, you can see each line written one by one, and this is rather unpleasant for the end user.

This menu should be displayed many times, so I wonder if I can somehow "cache" this output. If there wasn’t color text, I would just save the output in a line and write it at a time, but I don’t want to lose color information.

Any suggestions?

Update: Just to give an idea of ​​how intense the output is: Update 2: The sloppy code has been updated, but you still need to somehow drown out the output. MoveBufferArea is a partial solution (creates unwanted scrollbars)

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; 

Writing methods are just helper methods that encapsulate any behavior of the System.Console class, so we won’t need to set the color.

+4
source share
5 answers

Perhaps you could be smarter than using MoveBufferArea . How about if you had a buffer area that is currently not visible to the user. You can create your buffer \ menu, and then copy the entire area to the visible buffer part at a time. I don’t know how much control you have over the visible / invisible areas of the buffer, and if you can use them that way, this is just a suggestion.

The only other option I can think of is to create your own control that mimics the console. Then you can do buffered read / write or update only after making a few changes. But it can be a lot of extra work.

+2
source

Is there any way to move the attribute evaluation logic “out of loop?”? I suspect this is not the fastest part of your code.

+2
source

I don’t know if this is possible for your project, but one of the options uses some type of markup language to store the color inside the line itself, for example <yellow>Text in yellow</yellow> (or you can use a simple CSS selector or no difference) . This way you can use a simple line cache.

By the way, remember that the .NET Framework already implements one such cache: see String.Intern and String.IsInterned .

+1
source

Keep in mind that the Console is a custom device, which means you can write your own (although I do not recommend it). There is a way to specify in the configuration file in which the class performs the trace and which class performs the Debug output.

+1
source

Is there a way to ask your customers to make sure that they are using hardware accelerated graphics (check the drivers)? If they work with the default Microsoft driver for VGA only, then the console will be painfully slow.

+1
source

All Articles