How to use the individual console windows for the child console applications?

耗尽温柔 提交于 2020-02-04 03:54:05

问题


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):

How can I solve it?

UPD

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


回答1:


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:



来源:https://stackoverflow.com/questions/59770445/how-to-use-the-individual-console-windows-for-the-child-console-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!