✅ Why does Console.WindowWidth produce Invalid Handle when the app is invoked from Process?

I have two console application projects:
HelloWorld
and
Invoker
.

HelloWorld

try
{
    Console.Out.WriteLine(new string('=', Console.WindowWidth));
}
catch (Exception e)
{
    Console.Error.WriteLine($"HelloWorld's error: {e.Message}");
}

It works fine when executed directly.

Invoker

ProcessStartInfo info = new()
{
    FileName = "helloworld.exe",
    RedirectStandardOutput = true,
    RedirectStandardError = true,
};

using var process = new Process { StartInfo = info };
process.Start();

Task? outputTask = null;

if (!info.UseShellExecute && info.RedirectStandardError)
{
    var errorWriter = process.StandardError;
    Console.WriteLine($"Error: {errorWriter.ReadToEnd()}");
}

if(outputTask is not null) await outputTask;
await process.WaitForExitAsync();

It produces
Error: HelloWord's error: The handle is invalid.

What is wrong?
Was this page helpful?