【问题标题】:How to use the individual console windows for the child console applications?如何为子控制台应用程序使用单独的控制台窗口?
【发布时间】:2020-01-16 13:12:18
【问题描述】:

Windows 10、C#、.NET Core 3.1

我想要多个控制台窗口用于输出。例如,我想在一个显示器上放置一个仅显示错误输出的控制台窗口,在另一个显示器上,我想放置一组其他控制台窗口来显示各种报告。所有这些控制台窗口都是只读的。此外,同时我想拥有我将用作终端的主控制台窗口(用于关键字输入)。我在关于程序员的电影中看到了类似的东西,并想尝试做同样的事情:)

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

这是我的主要应用程序代码:

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

这是我的子应用程序代码:

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

结果是两个进程(父进程和子进程)的公共控制台窗口:

我该如何解决?

更新

我认为问题在于RedirectStandardInput 使用。我尝试寻找其他解决方案。

【问题讨论】:

    标签: c# .net-core process console-application


    【解决方案1】:

    我在 VS 2019 中尝试了以下内容。

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

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

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

    辅助控制台应用代码(在新控制台项目中):

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

    结果:

    【讨论】:

    • 没有。我看到您不使用RedirectStandardInput...但是您将如何从父进程写入子进程的输入流?我尝试使用RedirectStandardInput问题仅出现在这种情况下。子进程的生命周期也由其父进程管理。
    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 2011-06-04
    • 2011-01-20
    • 1970-01-01
    • 2016-03-30
    • 2011-04-20
    • 2014-02-17
    • 2011-04-04
    相关资源
    最近更新 更多