11
[DllImport("kernel32.dll")]
private static extern Int32 AllocConsole();

I can open cmd.exe with this command. But i can open only one console window and write in it. How can i open another one? Is any clean and fast solution for opening two console windows?

senzacionale
  • 20,448
  • 67
  • 204
  • 316

4 Answers4

18

So you can do multiple console windows within one single C# windows app, but to do so you would have to have a few things. Process.start(), and command-line parameters.

If you do it this way you can have your app create another instance of itself, but with different command line parameters to have each part do different things.

Here is a simplistic example of how to do it.

    namespace Proof_of_Concept_2
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length!= 0)
                {
                    if (args[0] == "1")
                    {
                        AlternatePathOfExecution();
                    }
                    //add other options here and below              
                }
                else
                {
                    NormalPathOfExectution();
                }
            }


            private static void NormalPathOfExectution()
            {
                Console.WriteLine("Doing something here");
                //need one of these for each additional console window
                System.Diagnostics.Process.Start("Proof of Concept 2.exe", "1");
                Console.ReadLine();

            }
            private static void AlternatePathOfExecution()
            {
                Console.WriteLine("Write something different on other Console");
                Console.ReadLine();
            }

        }
    }

Here is a screenshot of it working. enter image description here

In conclusion,

Getting 2 console windows is easy, getting them to talk to each other is a separate question in and of itself. But I would suggest named pipes. Relevant Stackoverflow Post

You have to change your thinking because the 2 Consoles once run on different processes don't automatically talk to each other. Whatever calculation you are doing on one of them, the other one is completely unaware.

Community
  • 1
  • 1
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • But then, how to communicate between the processes? – Mikael Dúi Bolinder Dec 05 '22 at 00:23
  • 1
    @MikaelDúiBolinder You can have them talk through WCF which has an API for interprocess communication. https://drdobbs.com/windows/interprocess-communication-with-wcf/196802111 – Alexander Ryan Baggett Dec 06 '22 at 04:35
  • That would be the best way to go. But there are of course more creative solutions. They could both talk to a database or message server. They could both take turns writing to a file. More realistically they could both call a Rest API. – Alexander Ryan Baggett Dec 06 '22 at 04:36
  • And simply forwarding the text input to the main process and getting the output from it? As an alternative to creating your own consoles using `TextBox` in a XAML app? – Mikael Dúi Bolinder Dec 07 '22 at 12:20
3

You can do

Process.Start("cmd.exe");

as many times as you would like. Is this what you mean?

jonnystoten
  • 7,055
  • 2
  • 28
  • 36
1

Unfortunately not, sorry — you cannot have more than one console window per application in Windows.

Timwi
  • 65,159
  • 33
  • 165
  • 230
  • Couldn't you just write an app in C++ that does this and import it into C#? See this post http://stackoverflow.com/questions/20847474/multiple-consoles-for-a-single-application-c – Alexander Ryan Baggett Jan 01 '15 at 00:11
0
  1. Run the Console app (first window)
  2. Got to bin > debug and open the YourApplication.exe file (second window)