Console.CancelKeyPress 无法正常工作

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

伊特诺亚

我有一个代码,如下所示

// 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(小 c),程序无法正常运行,并且

Cancel Key Pressed
在终端中没有看到,但是如果我按 Control + C(大写 c),程序正常工作,
Cancel Key is Pressed
出现在终端。

另一个奇怪的东西,如果我删除

if (line == "cancel")
   break;

我的代码行,再次调试运行程序,Control + c(小c)正常工作,Control + C(大写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/issues/ 上创建问题83869#issue-1638611037)

谢谢

c# .net console-application .net-6.0
© www.soinside.com 2019 - 2024. All rights reserved.