How to cancel console application?
-
I have a Windows Forms application that runs, when clicking on a button, a .exe console application. I know I can use
System::Diagnostics::Process::Kill
to exit it, and I want to do it when the user clicks on a Cancel button. The problem I am encountering is that while the console application is running it is not possible to interact with the Windows Forms application. Is there any way to run the process minimized or something similar? -
I have a Windows Forms application that runs, when clicking on a button, a .exe console application. I know I can use
System::Diagnostics::Process::Kill
to exit it, and I want to do it when the user clicks on a Cancel button. The problem I am encountering is that while the console application is running it is not possible to interact with the Windows Forms application. Is there any way to run the process minimized or something similar?piul wrote:
it is not possible to interact with the Windows Forms application.
It is, unless you made it impossible through some code. Show us the relevant code that deals with launching the console app. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
piul wrote:
it is not possible to interact with the Windows Forms application.
It is, unless you made it impossible through some code. Show us the relevant code that deals with launching the console app. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
There it goes:
proc= gcnew System::Diagnostics::Process; proc->StartInfo->RedirectStandardOutput = true; proc->StartInfo->UseShellExecute = false; //Necessary to redirect proc->StartInfo->FileName = "..\\\\..\\\\..\\\\Tests\\\\ConsoleTest\\\\debug\\\\ConsoleTest.exe"; proc->OutputDataReceived += gcnew System::Diagnostics:: DataReceivedEventHandler (&Form1::StdoutHandler); bool processStarted = false; try { processStarted = proc->Start(); } catch (Exception^ excp) { richTextBoxOutput->AppendText(excp->Message); } if (processStarted) { buttonCancel->Enabled = true; proc->BeginOutputReadLine();//Start reading the output proc->WaitForExit(); buttonCancel->Enabled = false; //... }
-
There it goes:
proc= gcnew System::Diagnostics::Process; proc->StartInfo->RedirectStandardOutput = true; proc->StartInfo->UseShellExecute = false; //Necessary to redirect proc->StartInfo->FileName = "..\\\\..\\\\..\\\\Tests\\\\ConsoleTest\\\\debug\\\\ConsoleTest.exe"; proc->OutputDataReceived += gcnew System::Diagnostics:: DataReceivedEventHandler (&Form1::StdoutHandler); bool processStarted = false; try { processStarted = proc->Start(); } catch (Exception^ excp) { richTextBoxOutput->AppendText(excp->Message); } if (processStarted) { buttonCancel->Enabled = true; proc->BeginOutputReadLine();//Start reading the output proc->WaitForExit(); buttonCancel->Enabled = false; //... }
piul wrote:
proc->WaitForExit();
this is a blocking call: the method waits, i.e. does not return, until the target process has exited. So if all the code shown is running on the main thread (say is part of an event handler, maybe a button click handler), then the main thread will sit there waiting , rather than handling all your Windows events (such as you clicking on the cancel button). There are a couple of ways to solve your problem, the one I prefer is using an extra thread, probably a BackgroundWorker. Its DoWork handler would contain
proc->BeginOutputReadLine();//Start reading the output
proc->WaitForExit();or even a synchronous loop to read the output, no need to use asynchronous when you already have a dedicated thread. BTW: you can't directly touch GUI Controls from inside DoWork, so you should enable the cancel button before you launch the BGW, and disable it inside the RunWorkerCompleted handler. One alternative would be to use a timer (a System.Windows.Forms.Timer at best), that ticks say every 300 msec. Its Tick handler could check (no wait!) whether the process has exited, and if it has, disable the cancel button and stop itself. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.