none
Writing Progress in console application at the same location RRS feed

  • Question

  • Hi,

    i do have a console application that should notify the user of a ongoing download progress. The output looks like this:

    Total download progress: 12% [32 MBytes so far]

    I am updating the "12% [32 MBytes so far]" part right after the "Total download progress:" part as soon as some data arrives from the network. I am using "Console.Write(text)" to write after the "Total download progress:" part and return to the point right after the colon with:

    System.Console.SetCursorPosition(                       
    System.Console.CursorLeft - text.Length,
    System.Console.CursorTop);  


    The thing here is that i get a "The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
    Parameter name: left
    Actual value was -26."

    Exception as soon as i change the console size or the text grows out of the consoles screen buffer or wraps to the next line as soons as i change the console size. How can i prevent this exception and how should i implement the text update routine and/or the cursor position handling? Any ideas would be nice.

    Thanks in advance

    K.

    "This reply is provided as is, without warranty express or implied"

    Thursday, September 6, 2018 9:51 PM

All replies

  • Is this the sort of thing you're after?

    using System;
    using System.Threading;
    
    namespace Project
    {
       class Program
       {
          static void Main(string[] args)
          {
             // Write the constantant part of the string.
             // Use Write(), not WriteLine(), so the cursor stays at the end of the line.
             Console.Write("Total download progress : ");
    
             // Get the location of the cursor. It should be at the end of the string.
             int left = Console.CursorLeft;
             int top = Console.CursorTop;
    
             // A loop to simulate progress.
             for (int i = 1; i < 11; i++)
             {
                // Set the cursor position to the same position each time.
                Console.SetCursorPosition(left, top);
    
                // Just some dummy values to illustrate the point.
                int percent = i * 10;
                int mbytes = i * 12;
                string progress = string.Format("{0}% [{1}MBytes so far]", percent, mbytes);
                Console.Write(progress);
    
                // Pause for half a second before the next write.
                Thread.Sleep(500);
             }
    
             Console.WriteLine("\n");
          }
       }
    }


    Thursday, September 6, 2018 11:29 PM
  • Hi Ante,

    thanks for your example, but it runs exactly into the same exception, if you resize the console while the output is written and the console and buffer width is less than the last cursor position:

    System.ArgumentOutOfRangeException
      HResult=0x80131502
      Message=The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
    Parameter name: left
    Actual value was 26.
      Source=<Cannot evaluate the exception source>
      StackTrace:
    <Cannot evaluate the exception stack trace>
    
    Its basically the same thing i am faced with. Try for yourself: Start the application and resize the console window into a smaller one beyond the colon (left and top data) while its running (use a lager counter variable and longer text) and you will get the exception as well.

    K.


    &quot;This reply is provided as is, without warranty express or implied&quot;

    Friday, September 7, 2018 12:20 AM
  • Sorry Kerem, but I don't understand what you mean by 'resize'. If I drag the borders of the console window using the mouse, I get scrollbars and no crash. Like so...

    I do get the error if I set the cursor position to out of bounds to start with, like...

    Console.SetCursorPosition(80, 20);

    But that is to be expected.

    Are you using some setting that prevents the console window from getting the scrollbars?

    Friday, September 7, 2018 12:47 AM
  • Hi Ante,

    >I do get the error if I set the cursor position to out of bounds to start with, like...

    >Console.SetCursorPosition(80, 20);

    >But that is to be expected.

    This is exactly what i am talking about. How do i prevent the out of bounds exception while dragging (resizing) the console window?

    K.


    &quot;This reply is provided as is, without warranty express or implied&quot;

    Friday, September 7, 2018 4:31 AM
  • I think we have a failure to communicate.

    Using the code I posted above, I do not get any error when I resize the window. As you can see in the screenshot I posted, the window has been resized to just a few characters wide and there is no error.

    If I add a line that sets the position to 80 or more, then I get the error. But I get that error at the start, not when resizing.

    Are you sure the code I posted causes the error during resizing? I can see how your code might do it, but mine does not (unless I'm missing something).


    • Edited by Ante Meridian Friday, September 7, 2018 5:19 AM Missed a question mark.
    Friday, September 7, 2018 4:58 AM
  • Check this approach too:

    for( int percent = 0; percent <= 100; percent += 10 )
    {
       Console.Write( "Total download progress: {0}%\r", percent );
       Thread.Sleep( 1000 );
    }
    Console.WriteLine();
    Console.WriteLine( "DONE." );

    or:

    for( int percent = 0; percent <= 100; percent += 10 )
    {
       Console.Write( "\rTotal download progress: {0}%...", percent );
       Thread.Sleep( 1000 );
    }
    Console.WriteLine( "\rDONE.                               " );
    


    Friday, September 7, 2018 5:15 AM