如何将单独的控制台窗口用于子控制台应用程序? [英] How to use the individual console windows for the child console applications?
问题描述
Windows 10、C#、.NET Core 3.1
我想要多个控制台窗口用于输出.例如,在一个显示器上,我想放置一个只显示错误输出的控制台窗口,在另一个显示器上,我想放置一组将显示各种报告的其他控制台窗口.所有这些控制台窗口都是只读的.此外,同时我想要主控制台窗口,我将用作终端(用于关键字输入).我在关于程序员的电影中看到了类似的内容,并想尝试做同样的事情:)
我希望我可以创建子进程并将父进程中的每个子进程写入 Input 中.我希望每个子进程都有自己的控制台窗口,但我看到他们使用主进程的控制台窗口.
这是我的主要应用程序代码:
使用系统;使用 System.Diagnostics;命名空间 ConsoleClient{课程计划{静态无效主(字符串 [] args){Console.Title = "控制台应用...";Console.WriteLine("这是主控制台应用程序的消息.");var procInfo = new ProcessStartInfo(@".loggerConsoleLogger.exe");procInfo.UseShellExecute = false;procInfo.RedirectStandardInput = true;procInfo.CreateNoWindow = false;使用 (var proc = new Process()){proc.StartInfo = procInfo;proc.Start();var sw = proc.StandardInput;sw.WriteLine("这是子控制台应用程序的消息.");Console.WriteLine("按回车键退出...");Console.ReadLine();proc.Kill();}}}}
这是我的孩子申请代码:
使用系统;使用 System.Diagnostics;命名空间 ConsoleLogger{课程计划{静态无效主(字符串 [] args){Console.Title = $"Process #{Process.GetCurrentProcess().Id} (logger)";而(真){var line = Console.In.ReadLine();Console.WriteLine(line);}}}}
结果是两个进程(父进程和子进程)的公共控制台窗口:
我该如何解决?
更新
我认为问题出在 RedirectStandardInput
使用中.我尝试寻找其他解决方案.
我在 VS 2019 中尝试了以下.
<块引用><块引用>我认为您的进程没有正确初始化:
主控制台应用程序代码(启动 2 个进程):
static void Main(string[] args){Console.Title = "主应用...";Console.WriteLine("这是主控制台应用程序的消息.");使用 (var process1 = new Process()){process1.StartInfo.UseShellExecute = true;process1.StartInfo.CreateNoWindow = false;process1.StartInfo.FileName = @"../../../../ConsoleAppAdditional/binDebug
etcoreapp3.1/ConsoleAppAdditional.exe";process1.Start();}使用 (var process2 = new Process()){process2.StartInfo.UseShellExecute = true;process2.StartInfo.CreateNoWindow = false;process2.StartInfo.FileName = @"../../../../ConsoleAppAdditional/binDebug
etcoreapp3.1/ConsoleAppAdditional.exe";process2.Start();}Console.WriteLine("按 ENTER (MainApp) 退出...");Console.ReadLine();}
辅助控制台应用程序代码(在新的控制台项目中):
static void Main(string[] args){Console.Title = $"Process #{Process.GetCurrentProcess().Id} (logger 附加)";{Console.WriteLine("新的附加应用程序启动..." + Process.GetCurrentProcess().Id);var line = Console.In.ReadLine();Process.GetCurrentProcess().Kill();}}
结果:
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(
@".loggerConsoleLogger.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.
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/binDebug
etcoreapp3.1/ConsoleAppAdditional.exe";
process1.Start();
}
using (var process2 = new Process())
{
process2.StartInfo.UseShellExecute = true;
process2.StartInfo.CreateNoWindow = false;
process2.StartInfo.FileName = @"../../../../ConsoleAppAdditional/binDebug
etcoreapp3.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:
这篇关于如何将单独的控制台窗口用于子控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!