nlstack01
nlstack01

Reputation: 839

Console.Writeline Cutting Off Large Output C#

I am simply trying to output a large amount of date to the console. Here is my code...

Main:

    TaskResult tr = task.ExecuteCheck();
          Console.WriteLine(task.Name + "....");
          Console.WriteLine(tr.Result);
          Console.WriteLine();
          Console.ReadLine();


ExecuteCheck
 TaskResult tr =new TaskResult();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(GetAlotOfData);
             sb.AppendLine(GetSomemoreData);
      tr.Result=sb.ToString();
            return tr;

When I run this the Console window only shows the last portion of the data eventhough tr.Result is populated correctly with all the data. I have never seen this behavior before. Any idea what could be happening here?

UPDATE:

Console.BufferHeight = Int16.MaxValue - 1; 

seems to get the data to show, but now the scroll bar is to touchy to use. You barely move it and everything is off the screen

How can this be optimized?

Upvotes: 0

Views: 2613

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

The console only keeps a limited buffer of records. It keeps up to Console.BufferHeight rows and Console.BufferWidth columns in memory. If your string is too wide to fit in the width or you do a NewLine it wraps it to a 2nd row. Once you fill up BufferHeight rows it starts dropping the oldest rows.

You can adjust the buffer height by assigning your own value to Console.BufferHeight, the largest allowable value is

Console.BufferHeight = Int16.MaxValue - 1;

A optimization you could potentially do is check Console.CursorTop after every write to the console, that will tell you which "row" you are on. Once you get to within some limit you grow the buffer dynamically.

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Some Stuff");
        GrowConsole();
    }

    const double TriggerGrowthPercentage = .90;
    const double GrowthPercentage = 1.25;
    private static void GrowConsole()
    {
        if (((double)Console.CursorTop)/Console.BufferHeight <= TriggerGrowthPercentage)
        {
            int growth = (int)(Console.BufferHeight*GrowthPercentage);
            growth = Math.Max(growth, 1); //Grow at least by 1;
            growth = Math.Min(growth, Int16.MaxValue - 1); //Don't grow bigger than Int16.MaxValue - 1;
            Console.BufferHeight = growth;
        }
    }

In the above example once the cursor moves to 90% of the buffer size it will grow the buffer 125% of it's current size.

Upvotes: 1

James Dev
James Dev

Reputation: 3009

Change the console buffer height:

Console.BufferHeight = Int16.MaxValue - 1;

Also try to set the Console window height to make it a bit more readable:

Console.WindowHeight = Console.LargestWindowHeight;

Upvotes: 0

Related Questions