NormD
NormD

Reputation: 549

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

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) which would send String1 to one console window 1 and Console.WriteLine(2, String2) which would send String2 to console window 2.

Upvotes: 5

Views: 16603

Answers (4)

Pedro Simões
Pedro Simões

Reputation: 237

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<int>(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();
            }
        }
    }
}

Output

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.

Upvotes: 5

JaredPar
JaredPar

Reputation: 754545

Is it possible to have 2 Windows Consoles in the same application?

No. The Console class is just a very thin wrapper around the Win32 idea of a console. Essentially everything you see when you use cmd.exe. There is no way to create 2 of these in a single process.

Could I have 2 Console-like windows?

Yes. It would be possible to essentially build a very console like class in WPF or WinForms. Use that to spit out text to a command line like application.

Upvotes: 5

tvanfosson
tvanfosson

Reputation: 532445

Rather than multiple consoles you might want to consider the idea of multiple log appenders. I'm not a real big fan of printf debugging, but I do sometimes see a place for multiple log files. Using something like log4net, you can configure multiple appenders and loggers that correspond to different classes/namespaces and log these to separate files. You could use the logging information to help debug using something like TextPad to view the log file(s) while debugging, since TextPad, unlike NotePad, detects and allows you to view the contents of any updates done while the file is open. There may be other editors that allow this, too, but I'm familiar with TextPad.

Upvotes: 2

Logan Capaldo
Logan Capaldo

Reputation: 40336

Nope

A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console.

[http://msdn.microsoft.com/en-us/library/ms681944(VS.85).aspx](http://msdn.microsoft.com/en-us/library/ms681944(VS.85).aspx)

Instead you can use WinForms or WPF to create multiple "console" windows and write a simple WriteLine method to append text to a text box or similar.

Another option is to open a log file, and use that as your second "console".

Upvotes: 6

Related Questions