Console.CancelKeyPress 无法正常工作

问题描述 投票:0回答:1

我有代码,如下所示

// See https://aka.ms/new-console-template for more information
using System;
using System.Diagnostics;
using System.IO;

namespace ForkTest
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Parent: Hello, World!");

            const string CHILD_PROGRAM_ADDRESS = @"..\..\..\..\ChildProgramTest\bin\Debug\net7.0\ChildProgramTest.exe";

            if (!File.Exists(CHILD_PROGRAM_ADDRESS))
            {
                throw new FileNotFoundException(Path.GetDirectoryName(path: System.Reflection.Assembly.GetExecutingAssembly().Location));
            }

            var process = new Process();
            process.StartInfo = new ProcessStartInfo(CHILD_PROGRAM_ADDRESS, "")
            {
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            process.ErrorDataReceived += (sender, e) =>
            {
                Console.WriteLine("Parent: " + e.Data);
            };

            process.OutputDataReceived += (sender, e) =>
            {
                Console.WriteLine("Parent: " + e.Data);
            };

            process.EnableRaisingEvents = true;

            process.Start();

            StreamWriter childStandardInput = process.StandardInput;
            StreamReader childStandardOutput = process.StandardOutput;
            StreamReader childStandardError = process.StandardError;

            if (Console.IsInputRedirected || Console.IsOutputRedirected || Console.IsErrorRedirected)
            {
                Console.WriteLine("Parent: Console is Redirected!");
            }

            Console.CancelKeyPress += (sender, e) =>
            {
                Console.WriteLine("Cancel Key is Pressed");
                Console.Write(childStandardOutput.ReadToEnd());
                Console.Error.Write(childStandardError.ReadToEnd());

                childStandardInput.Close();
                childStandardOutput.Close();
                childStandardError.Close();
            };

            string line;
            while ((line = Console.ReadLine()) != null)
            {
                childStandardInput.WriteLine(line);
                childStandardInput.Flush();

                if (line == "cancel")
                    break;
            }

            process.WaitForExit();
        }
    }
}

Visual Studio 2022 中运行 Debug 并按 Control + C 后,程序无法正常运行,并且 Cancel Key Pressed 在终端中看不到,但是如果我按 Shift + Control + C,程序运行正常,终端出现Cancel Key is Pressed

另一个奇怪的事情:如果我删除

if (line == "cancel")
   break;

我的代码中的行,并再次调试运行程序,Ctrl + C 工作正常,Shift + Ctrl + C 工作不正常。

我的项目是 .NET 7 中的控制台应用程序(我使用 .NET 6 进行测试,结果相同)和 Visual Studio 2022 17.4.6 中的调试 (F5)。

我该如何解决这个问题?

(我还在 https://developercommunity.visualstudio.com/t/ConsoleCancelKeyPress-does-not-working/10320196 上添加了一张票,并在 https://github.com/dotnet/runtime/ 上创建了一个问题问题/83869#issue-1638611037)

c# .net console-application .net-6.0
1个回答
0
投票

你的代码工作正常,我用一个简单的

process
作为孩子
process
.

测试过它

运行项目后控制台窗口未获得焦点,因此键盘事件未正确传递。您应该手动激活它。这是我面临的唯一问题。

© www.soinside.com 2019 - 2024. All rights reserved.