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

Đang mở
#138,088 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
operating-systems

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C#
Star
18.5k
Fork
5.8k
Merge trung bình
2 ngày 18 giờ
Pull request đã merge (30 ngày)
588

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/runtime

Tất cả issue của dotnet/runtime

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.