Can you have multiple .net Consoles (as in Console.Writeline)

后端 未结 4 900
無奈伤痛
無奈伤痛 2020-12-11 06:59

It would be handy during debugging to have multiple consoles (not multiple monitors, which I already have). I am imagining something like Console.WriteLine(1, String1) whic

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 07:33

    I had the same need and I found your question here in SO. A lot of years later :)

    In fact, .NET doesn't have this capability, but we can simulate it by splitting the screen with a grid, using some buffering.

    Here is an example:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void print(int x, int y, int n)
            {
                for(int i = 0; i < 20; i++)
                {
                    Console.CursorLeft = x; Console.CursorTop = y++;
                    Console.Write(new string(n.ToString().Last(), 60));
                }
            }
    
            static void Main(string[] args)
            {
                var l = new List(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    
                while (true)
                {
                    Console.Clear();
                    Console.CursorVisible = false;
    
                    for (int i = 0; i < l.Count; i++)
                    {
                        print((i % 3) * 60, (i / 3) * 20, l[i]++);
                    }
    
                    Task.Delay(1000).Wait();
                }
            }
        }
    }
    

    So the screen is divided into a 3x3 grid and each screen is updated using the cursor position.

    It could be packaged into a lib and a generic API, like you described:

    Console.WriteLine(1, String1)
    Console.WriteLine(2, String2)
    

    Hope it helps someone.

提交回复
热议问题