c# - 抑制子进程的输出
问题描述
假设我们使用以下命令启动控制台应用程序:
public static void StartProcess()
{
using var next = new Process();
next.StartInfo.UseShellExecute = false;
next.StartInfo.FileName = "dotnet";
next.StartInfo.Arguments = "/opt/ConsoleApp1/ConsoleApp1.dll";
next.Start();
}
这段代码导致 double StandardOutput
and StandardError
,因为父进程和子进程会将数据写入同一个终端。如何抑制子进程输出和/或分离子控制台?
当然我可以这样做:
public static void StartProcess()
{
using var next = new Process();
next.StartInfo.UseShellExecute = false;
next.StartInfo.FileName = "dotnet";
next.StartInfo.Arguments = "/opt/ConsoleApp1/ConsoleApp1.dll";
next.StartInfo.RedirectStandardOutput = true;
next.StartInfo.RedirectStandardError = true;
next.Start();
next.StandardOutput.BaseStream.CopyToAsync(Stream.Null);
next.StandardError.BaseStream.CopyToAsync(Stream.Null);
}
据我了解,这将一直有效,直到父进程还活着,但是如果子进程的工作时间比父进程长怎么办?需要一些稳定的跨平台解决方案。
解决方案
推荐阅读
- node.js - 如何获取特定不和谐服务器的消息作者昵称
- android - 使用 AsyncTask 或 Alternative 从 sqlite 数据库中获取数据。下面是 databasehelper 类方法如何为它创建 asyncTask 进程谢谢
- reactjs - 如何将 next-auth 身份验证提供程序用于其他第三方应用程序
- javascript - 在反应路由器dom v5中配置嵌套路由时遇到问题
- sql - 编写此 TSQL 语句的更好方法
- python - 如何使用 python 和 opencv 使用 cv2.estimateAffine2D()?
- javascript - Vue.js Typescript 我使用 getter 获取数据,但无法在方法中访问它
- git - `git rebase -i HEAD~N` 和 `git reset --soft HEAD~N` 有什么区别?
- node.js - 使用猫鼬检索数组中的对象信息
- c# - 如何将对象绑定到事件,以便在触摸时在 SkiaSharp 中调用事件?