0

I have a program that displays an estimated time remaining, but also prints other things while it does this. I want to print the other parts without affecting the position of the timer.

here is my sample output:

Processing the files into smaller chunks ...

90% 00:00:01 left.

Finished processing files
Now Comparing ...

AffiliatePairoffSnapshot
10% 00:00:08 left.
        Total:               0

AllocationSnapshot
23% 00:00:07 left.
.trans.links[0].type.TransLinkType                                                 102

.trans.links[0].fromTransactionRef.versionHandle.instanceHandle.id                 102

        Total:             204

TradeSnapshot

        Total:               0

TransferSnapshot
86% 00:00:01 left.
.trans.links[0].size.amount                                                          5

.trans.links[0].fromTransactionRef.versionHandle.instanceHandle.id                   5

.trans.activityContextRef[0].versionHandle.instanceHandle.id                      3708

        Total:            3718

        Total Number Different Fields Across All ObjectTypes:            3922

The Generated xml files can be found at: c:\Users\grover\packer\XML

I want to print the time left on its own line at the to and not have the other prints affected is there a way to do this?

Servy
  • 202,030
  • 26
  • 332
  • 449
jg943
  • 309
  • 3
  • 21

2 Answers2

0

If you really needed to do it, you could overwrite what was already written to the console, but that would make a pretty lousy user experience unless it updates infrequently

Community
  • 1
  • 1
0

Have the code printing the times do the following:

  1. Store the current cursor position in a variable.
  2. Move the cursor to the next line.
  3. Each time you want to update the text:
    1. Store the current cursor position in another variable.
    2. Set the cursor to the line the cursor was on in the first step.
    3. Write some text.
    4. Set the current cursor position to the value stored in step 3.1.

Here's a simple demo of the concept:

private static void Main()
{
    PrintTimes();

    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("Some text is being printed");
        Thread.Sleep(1000);
    }

    Console.WriteLine();
    Console.WriteLine("press any key to exit . . .");
    Console.ReadKey();
}

private static async void PrintTimes()
{
    int line = Console.CursorTop;
    Console.WriteLine();
    for (int i = 0; i < 10; i++)
    {
        int previousLine = Console.CursorTop;
        int previousChar = Console.CursorLeft;
        Console.CursorTop = line;
        Console.CursorLeft = 0;
        Console.WriteLine("{0}% left       ", 100 - i * 10);
        Console.CursorTop = previousLine;
        Console.CursorLeft = previousChar;

        await Task.Delay(1000);
    }
}

Note that in this demo the word done in PrintTimes is not atomic, so it's still possible for another console write to be injected in between some of those actions. If you end up writing to the console frequently you'll need to ensure to synchronize all console writes.

Servy
  • 202,030
  • 26
  • 332
  • 449