Open two console windows with C #

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

I can open cmd.exe with this command. But I can only open one console window and write in it. How can I open another one? Is there any clean and quick solution to open two console windows?

+4
source share
3 answers

Unfortunately, no, I'm sorry - you cannot have more than one console window for each application on Windows.

+1
source

So, you can do multiple console windows in a single Windows C # application, but you need to have several things to do this. Process.start () and command line options.

If you do this like this, you can create your own application to create another instance, but with different command line options so that each part does different things.

Here is a simplified example of how to do this.

  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 his work. enter image description here

Finally,

Getting two console windows is easy, getting them to talk to each other is a separate issue in itself. But I would suggest the named pipes. fooobar.com/questions/66835 / ...

You have to change your mindset because 2 consoles, once running on different processes, do not automatically talk to each other. No matter what kind of calculation you make on one of them, the other is completely unaware.

+5
source

You can do

 Process.Start("cmd.exe"); 

as many times as you want. Is that what you mean?

+3
source

All Articles