'Running multiple tasks parrallel and overwriting each line in for

I'm pretty new to programming, but I have a question:

How could I show different task output (I should use the tasks) in different lines, in the console? Also, I want to overwrite the lines each time , so the output should show:

i // overwriting this line everytime.

j // overwriting this line everytime.

I tried using SetCursorPosition, but it makes a collision between the task ouput somewhy.

A mini code sample (hope this makes it clearer):

Task a = Task.Run(() =>
{
    int i;
    for (i = 0; i <= 1000000; i++)
    {
        Console.SetCursorPosition(Console.CursorLeft, 0); // ? 
        Console.WriteLine("{0}", i);
    }
});

Task b = Task.Run(() =>
{
    int j;
    for (j = 0; j <= 1000000; j++)
    {
        Console.SetCursorPosition(Console.CursorLeft, 3); // ?
        Console.WriteLine("{0}",j);
    }
});

Console.ReadLine();

Thank You in advance :)



Solution 1:[1]

You need to synchronize access to Console for different tasks (I am not fixing position part if there is any problem, but an obvious synchronization problem):

var consoleLock = new object();

Task.Run(() =>
{
    for (int i = 0; i <= 1000000; i++)
    {
        Thread.Sleep(1); // to simulate some work
        lock(consoleLock)
        {
            Console.SetCursorPosition(Console.CursorLeft, 0);
            Console.WriteLine(i);
        }
    }
});

Task.Run(() =>
{
    for (int j = 0; j <= 1000000; j++)
    {
        Thread.Sleep(1); // to simulate some work
        lock(consoleLock)
        {
            Console.SetCursorPosition(Console.CursorLeft, 3);
            Console.WriteLine(j);
        }
    }
});

Console.ReadLine();

Solution 2:[2]

This is a simplified demo of what you are trying. Modify it as per your needs.

The basic idea is, you call a ThreadSafe method to write to console from all threads. And, every time you update the console, you re-write the two lines (notice the use of SetCursorPosition() method).

static object locker = new object();
static int iCommon = 0;
static int jCommon = 0;

static void WriteText()
{
    lock(locker)
    {
        var text = string.Format("i = {0}{1}j = {2}", 
                                    iCommon, Environment.NewLine, jCommon);
        Console.SetCursorPosition(0, 
                                  Console.CursorTop == 0 ? 0 : Console.CursorTop - 1);
        Console.Write(text);
    }
}

static void Main(string[] args)
{
    Task a = Task.Run(() =>
    {
        for (int i = 0; i <= 15; i++)
        {
            Thread.Sleep(700); //just to demonstrate the display change
            iCommon = i;
            WriteText();
        }
    });

    Task b = Task.Run(() =>
    {
        for (int j = 0; j <= 10; j++)
        {
            Thread.Sleep(1150); //just to demonstrate the display change
            jCommon = j;
            WriteText();
        }
    });
    Console.ReadLine();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2