如何使用ConsoleCancelEventHandler几次几次、如何使用、ConsoleCancelEventHandler

2023-09-04 11:07:25 作者:只要你要,只要我有

我一直忙于编写一个应用程序,其功能如同一个前端:它有一个图形用户界面正与按钮等任务的命令行选项并将它们传递到命令行.exe文件。它使用该应用程序的控制台显示命令行应用程序的输出。 这工作得很好,但使用Ctrl + C或试图关闭控制台窗口时,在GUI关闭过,这是不是真的是我想要的。然而,让程序输出与它自己的控制台是不可能的,因为它批量处理文件,每个文件将弹出它自己的控制台。

I've been busy coding a application that functions as a frontend: it has a GUI taking command line options with buttons and things like that and passing them to a command line .exe. It uses the console of the application to display the output of the command line app. This works fine, but when using Ctrl+C or trying to close the console window, the GUI closes too, which is not really what I want. However, letting the program output with it's own console is not possible because it batch-processes files and every file would pop up it's own console.

该程序是用C ++编写用MSVC 2012,并使用.NET。我试着控制台:CancelKey preSS至少得按Ctrl + C的行为,因为我希望它(停止命令行应用程序,但没有图形用户界面),但有一些这方面的麻烦。

The program is written in C++ with MSVC 2012 and uses .NET. I tried Console::CancelKeyPress to at least get Ctrl+C behave as I want it to (stop the command-line app but not the GUI) but have some trouble with this.

我的code

private: System::Void OnCancelKeyPressed(System::Object^  sender, System::ConsoleCancelEventArgs^  e) {
         e->Cancel = true;
     }
private: System::Void GetConsoleReady() {
         COORD c;
         FreeConsole();
         AllocConsole();
         c.X = 80; c.Y = 8000;
         SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),c);
         Console::Clear();
         Console::TreatControlCAsInput = false;
         Console::CancelKeyPress += 
             gcnew ConsoleCancelEventHandler(this, &Form1::OnCancelKeyPressed);  
     }

这被称为每一个用户试图运行一个批处理文件来处理时间。运行批处理后,控制台被释放FreeConsole()。第一次它的作品不错,使用Ctrl + C杀死在命令行应用程序,但在GUI中继续处理,运行其他命令,最后用FreeConsole()。然而,试图做到这第二次的时候,它杀死了图形用户界面,以及。我试图增加新的事件删除previous事件之前添加此

This gets called every time a user tries to run a batch of files to process. After running the batch, the console is released with FreeConsole(). The first time it works nice and using Ctrl+C kills the command line app but the processing in the GUI continues, running other commands and finally using FreeConsole(). However, when trying to do this a second time, it kills the GUI as well. I tried to add this before adding the new event to remove the previous event

             Console::CancelKeyPress -= 
             gcnew ConsoleCancelEventHandler(this, &Form1::OnCancelKeyPressed);  

但不知何故,上添加处理程序引发错误,但只有第二次:未处理的异常类型的'System.IO.IOException'出现在mscorlib.dll,其他信息:德参数onjuist

But somehow that raises an error on adding the handler, but only the second time: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll, Additional information: De parameter is onjuist.

这最后一部分是荷兰人为错误的参数,调试器说,扼流圈readding的ConsoleCancelEventHandler。

That last part is Dutch for 'wrong argument', and the debugger says it chokes on readding the ConsoleCancelEventHandler.

如果我尝试将其添加到添加事件处理只有一次形式加载时,它什么都不做。

If I try to add the event handler only once by adding it when loading of the form it does nothing.

什么是怎么回事?

推荐答案

你有没有考虑在不同的进程中运行该批处理?然后,您可以通过从衍生进程的标准输出(和stderror)读取显示批处理的输出。

Have you considered running the batch in a different process? You can then display the output of the batch process by reading from the stdout (and stderror) of the spawned process.

如果您使用的是.NET再看看Process.RedirectStandardOutput(例如此处Capturing从.NET应用程序的控制台输出(C#))。

If you are using .net then take a look at Process.RedirectStandardOutput (example here Capturing console output from a .NET application (C#)).

如果您隐藏衍生的进程那么有没有办法让用户与其交互/关闭它。你的主要应用仍处于完全的控制,也没有问题,CTRL-C等。

If you hide the spawned process then there is no way for the user to interact with it / close it. Your main application remains in complete control and there are no issues with ctrl-c etc.