Process.Run/RunAsync(silent: true) still creates a console window when the parent is a GUI app

未关闭
#138,088 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp

调研方向

The silent overloads in src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Scenarios.cs (around lines 202-211) only set the standard handles. CREATE_NO_WINDOW is applied only from startInfo.CreateNoWindow in SafeProcessHandle.Windows.cs (around line 283). Start by reading how that flag is chosen, then decide how silent should set it, possibly only when the parent has no console. Done when a silent child started from a WinExe parent shows no console window, and console parents keep their Ctrl+C behaviour.

由索引模型根据 Issue 内容生成。

描述

area-System.Diagnostics.Process
Description

@adamsitnik

Process.Run and Process.RunAsync with silent: true redirect stdin/stdout/stderr to the null device, but don't set CreateNoWindow.

When the parent process has no console (e.g. a WinExe/GUI app), Windows allocates a new console for a console subsystem child, so you get an empty console window that flashes up for the lifetime of the child process. The child produces no output, as expected, but the window still appears.

I can't think of a scenario where someone would pass silent: true and want an empty console window to appear. I hit this in a GUI app (an IDE) that runs dotnet and git processes in the background.

Reproduction Steps

In a WinExe app (<OutputType>WinExe</OutputType>):

await Process.RunAsync("ping.exe", ["-n", "4", "127.0.0.1"], silent: true);

(ping is just used as a ~3 second sleep - timeout exits immediately when stdin is redirected to the null device.)

Expected behavior

I would expect that no console window is shown. I can't think of any scenario where you would want to redirect the standard handles to null, but also show a console window? (Which would show no output).

Actual behavior

An empty console window is shown until the child process exits.

Regression?

No, new API in .NET 11.

Known Workarounds

Use the ProcessStartInfo overload and set the handles and CreateNoWindow manually:

using SafeFileHandle nullHandle = File.OpenNullHandle();
var startInfo = new ProcessStartInfo("ping.exe", ["-n", "4", "127.0.0.1"])
{
    CreateNoWindow = true,
    StandardInputHandle = nullHandle,
    StandardOutputHandle = nullHandle,
    StandardErrorHandle = nullHandle,
};
await Process.RunAsync(startInfo);
Configuration

.NET 11.0.0 (SDK 11.0.100), also present on main. Windows 11, x64. Specific to Windows. Only reproduces when the parent has no console - from a console app, the child shares the parent's console so no window appears, which is likely why this wasn't noticed.

Other information

The silent overloads only set the standard handles:

https://github.com/dotnet/runtime/blob/d7a6547f2a039031d9c29180bb95541a06a86276/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Scenarios.cs#L202-L211

CREATE_NO_WINDOW is only ever set from startInfo.CreateNoWindow:

https://github.com/dotnet/runtime/blob/d7a6547f2a039031d9c29180bb95541a06a86276/src/libraries/System.Diagnostics.Process/src/Microsoft/Win32/SafeHandles/SafeProcessHandle.Windows.cs#L283

silent was added in #129509 (API approved in #128453). Neither discussed console windows.

StartAndForget has the same behaviour - it redirects to the null device by default, but would still create a window.

One consideration - with CREATE_NO_WINDOW, a child of a console parent gets its own hidden console rather than sharing the parent's, so it would no longer receive Ctrl+C from the parent's console. Setting it only when the parent has no console (GetConsoleWindow() == IntPtr.Zero) would avoid changing behaviour for console parents.

主要语言
C#
星标
18.5k
派生
5.8k
平均合并
2 天 18 小时
30 天内合并 PR
588

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/runtime 的其他 Issue

查看 dotnet/runtime 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。