2

Windows 10, C#, .NET Core 3.1

I want to have multiple console windows for output. For example, on one display I want to place the one console window which will display errors only output, on other display I want to place the set of other console windows which will display various reports. All these console windows are to be read only. Also, at the same time I want to have the main console window which I will use as the terminal (for keyword input). I saw the similar in the films about programmers and want try to do the same :)

I expected I can create the child processes and write into Input each of them from the parent process. I expected each child process will have its own console window, but I see they use the console window of the main process.

This is my main application code:

using System;
using System.Diagnostics;

namespace ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Console app...";

            Console.WriteLine("This is the message for the main console application.");

            var procInfo = new ProcessStartInfo(
                @".\logger\ConsoleLogger.exe");

            procInfo.UseShellExecute = false;
            procInfo.RedirectStandardInput = true;
            procInfo.CreateNoWindow = false;

            using (var proc = new Process())
            {
                proc.StartInfo = procInfo;
                proc.Start();

                var sw = proc.StandardInput;

                sw.WriteLine("This is the message for the child console application.");

                Console.WriteLine("Press ENTER for exit...");
                Console.ReadLine();
                proc.Kill();
            }
        }
    }
}

This is my child application code:

using System;
using System.Diagnostics;

namespace ConsoleLogger
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = $"Process #{Process.GetCurrentProcess().Id} (logger)";
            while (true)
            {
                var line = Console.In.ReadLine();
                Console.WriteLine(line);
            }
        }
    }
}

The result is the common console window for both processes (parrent and child):

enter image description here

How can I solve it?

UPD

I think the problem is in RedirectStandardInput using. I try to find other solution.

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182

1 Answers1

0

I tried the following in VS 2019.

I presume your processes are not initialized correctly:

Main console application code (starts 2x processes):

static void Main(string[] args)
    {
        Console.Title = "Main app...";
        Console.WriteLine("This is the message for the main console application.");

        using (var process1 = new Process())
        {
            process1.StartInfo.UseShellExecute = true;
            process1.StartInfo.CreateNoWindow = false;
            process1.StartInfo.FileName = @"../../../../ConsoleAppAdditional/bin\Debug\netcoreapp3.1/ConsoleAppAdditional.exe";
            process1.Start();
        }

        using (var process2 = new Process())
        {
            process2.StartInfo.UseShellExecute = true;
            process2.StartInfo.CreateNoWindow = false;
            process2.StartInfo.FileName = @"../../../../ConsoleAppAdditional/bin\Debug\netcoreapp3.1/ConsoleAppAdditional.exe";
            process2.Start();
        }

        Console.WriteLine("Press ENTER (MainApp) for exit...");
        Console.ReadLine();
    }

Secondary console app code (in a new console project):

        static void Main(string[] args)
    {
        Console.Title = $"Process #{Process.GetCurrentProcess().Id} (logger additional)";
        {
            Console.WriteLine("New additionnal app started... " + Process.GetCurrentProcess().Id);
            var line = Console.In.ReadLine();
            Process.GetCurrentProcess().Kill();
        }
    }

Result: Result of multiple consoles

KFactory
  • 520
  • 3
  • 10
  • No. I see you don't use `RedirectStandardInput`... But how do you will write into the input stream of the child processes from the parent? I've tried to use `RedirectStandardInput` for it. *The problem appears at this case only*. Also lifetime of the child processes are to be managed by its parent. – Andrey Bushman Jan 17 '20 at 07:06