How to use ConsoleCancelEventHandler several times

♀尐吖头ヾ 提交于 2019-12-10 13:48:27

问题


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.

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.

My 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);  
     }

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);  

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.

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.

What is going on here?


回答1:


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.

If you are using .net then take a look at Process.RedirectStandardOutput (example here Capturing console output from a .NET application (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.



来源:https://stackoverflow.com/questions/16101517/how-to-use-consolecanceleventhandler-several-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!