【发布时间】: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