Search code examples
c#multithreadingconsoleconsole-applicationthread-sleep

How Can i Print 2 Works by threads side by side in same column in c# Console?


So Here is the Program Again As u can see i have Created 2 methods with a Working Loop And Created 2 threads in main pointing towards these methods and they are started

What gets out as output is Both Loops work like 1 and then space and in new line 1 and so on But what i want is to make them appear in the same row line side by side As we divide a page in 2 parts and write things in lines

I do not want To make them Work Seperately but at a time and in the same line but Different columns

I know it can be acheived by Writing Both Objects in same Console .wl but i want to acheive it this way by these 2 threads

Please provide valuable solutions that would work Thanks

using System; using System.Threading;

class Program
{
static void Main(string [] args) 
{
 Thread t1 = new Thread(code1);
 Thread t2= new Thread (code2);
 t1.Start();
 t2.Start();
}
static void code1()
{
 for(int i=0;i<50;i++)
 {
 Console.WriteLine(i);
 Thread.Sleep(1000);
 }
}
static void code2()
{
 for(int i=0;i<50;i++)
 {
 Console.WriteLine("/t/t"+i);
 Thread.Sleep(1000);
 }
}}

Solution

  • You have to use the Console.SetCursorPosition(int left, int top) method, so you can write on the Console starting from any position you want, also back in the previous rows.

    Obviously, you have to keep trace of the position for each Thread. That is, the current row of that Thread, and its first column. In my example I made 2 threads, one with the first column in position 0, and the second with the first column in position 50. Be careful about the width of the strings that you need to write, or they will overflow their own space on the Console.

    Also, because you are doing it in a multithreading app, you need a lock on the Console. Otherwise, suppose this: a Thread sets the CursorPosition, then another Thread sets it, then the scheduler returns to the first Thread... the first Thread writes on the second Thread's position!

    This is a very simple Console Program that gets the point:

    using System;
    using System.Threading;
    
    namespace StackOverflow_3_multithread_on_console
    {
        class Program
        {
            static Random _random = new Random();
    
            static void Main(string[] args)
            {
                var t1 = new Thread(Run1);
                var t2 = new Thread(Run2);
    
                t1.Start();
                t2.Start();
            }
    
            static void Run1()
            {
                for(int i = 0; i < 30; i++)
                {
                    Thread.Sleep(_random.Next(2000)); //for test
                    ConsoleLocker.Write("t1:" + i.ToString(), 0, i);
                }
            }
    
            static void Run2()
            {
                for (int i = 0; i < 30; i++)
                {
                    Thread.Sleep(_random.Next(2000)); //for test
                    ConsoleLocker.Write("t2:" + i.ToString(), 30, i);
                }
            }
        }
    
        static class ConsoleLocker
        {
            private static object _lock = new object();
    
            public static void Write(string s, int left, int top)
            {
                lock (_lock)
                {
                    Console.SetCursorPosition(left, top);
                    Thread.Sleep(100); //for test
                    Console.Write(s);
                }
            }
        }
    }
    

    All the Thread.Sleep are there just to demonstrate that the lock works well. You can remove all them, especially the one in the ConsoleLocker.