CybeX
CybeX

Reputation: 2396

c# - new cmd process writing to existing allocated console

My project I am creating in C# makes use of multithreading, my Console Application solution uses +2 projects, 1 as a "main" project, then sub projects as the program's actual functions combined in 1 solution (with approriate references)

I know that for each process, only 1 console can be used, thus to my understanding, I should create a new cmd.exe process and assign this paticular thread's output to this console

problem: creating a simple process is easy with cmd.exe, it creates a new cmd console over the existing, thus 2 consoles. But reassigning the standard output,etc (ref code below). in the new sub thread program (aka not the main application), it starts to write in the existing console and not in the newly created console thus only 1 console visible,

I want the exising one visible aswel as the new console with the appropriate output

am I missing something?

p.s. I am learning threading as well as this being a side project

        Process psi = new ProcessStartInfo("cmd.exe")
        {
            RedirectStandardError = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = false,
        };

        p = Process.Start(psi);

        Streamwriter sw = p.StandardInput;
        Streamreader sr = p.StandardOutput;

Upvotes: 2

Views: 382

Answers (2)

CybeX
CybeX

Reputation: 2396

I resorted to using a thread to check for updates and redirecting the standard input/output as mentioned above

Upvotes: 0

JohnKiller
JohnKiller

Reputation: 2008

This just can't be done that way.

An instance of your program, even if is a console, is not a cmd window. It's a console.

Also, to work the way you want, you'll need to open the StreamWriter to the StandardOutput stream, since you want to output data... and that's not allowed by design.

The window is owned by its process, not its threads, so 1 process = 1 console window. If you want more console windows, use multiple processes and make them comunicate using interop.

Upvotes: 1

Related Questions