Suppress output of child process

Viewed 180

Let's say we start console application with:

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

This code leads to double StandardOutput and StandardError, because parent and child processes will write data to the same terminal. How to supress child process output and/or detach child console?

Of course I can do something like this:

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

As far as I understand this will work until parent process is alive, but what if child process will work longer than parent one? Need some stable, cross-platform solution.

1 Answers

Adding StartInfo.CreateNoWindow = true fixed it for me.

Well, almost... Starting the process still spits out a newline onto the Linux console, but no other output.

Related