How to use ConsoleCancelEventHandler several times

I was busy coding an application that functions as an interface: it has a graphical interface that uses command line options with buttons, etc. and transferred to the command line .exe. It uses the application console to display the output of the command line application. This works fine, but when using Ctrl + C or trying to close the console window, the GUI closes too, which I don't want. However, it is impossible to allow a program to bring its own console with itself, because it processes files in batches and each file will pop up in its own console.

The program is written in C ++ with MSVC 2012 and uses .NET. I tried the :: CancelKeyPress console to at least make Ctrl + C behave the way I want (stopping the command line application, but not the GUI), but I have some problems 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);  
     }

It is called every time a user tries to run a package of files for processing. After starting the package, the console will be released using FreeConsole (). The first time it works well, and Ctrl + C kills the command line application, but processing in the GUI continues, launches other commands and finally uses FreeConsole (). However, when you try to do this a second time, it also kills the GUI. I tried adding this before adding a new event to remove the previous event

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

- , : "System.IO.IOException" mscorlib.dll, : De onjuist.

" ", , ConsoleCancelEventHandler.

, , .

?

+5
1

? , stdout ( stderror) .

.net, Process.RedirectStandardOutput( .NET(#)).

, / . , ctrl-c ..

0

All Articles