如何将单个控制台窗口用于子控制台应用程序? [英] How to use the individual console windows for the child console applications?

查看:152
本文介绍了如何将单个控制台窗口用于子控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 10,C#、. NET Core 3.1

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

我希望我可以创建子进程并将其从父进程写入Input中.我希望每个子进程都有自己的控制台窗口,但是我看到它们使用主进程的控制台窗口.

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

我该如何解决?

UPD

我认为问题出在RedirectStandardInput使用中.我试图找到其他解决方案.

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

推荐答案

我在VS 2019中尝试了以下方法.

I tried the following in VS 2019.

我认为您的进程未正确初始化:

I presume your processes are not initialized correctly:

主控制台应用程序代码(启动2个进程):

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();
        }
    }

结果:

这篇关于如何将单个控制台窗口用于子控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆