Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to cancel console application?

How to cancel console application?

Scheduled Pinned Locked Moved Managed C++/CLI
winformshelptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • P piul

      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?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      P 1 Reply Last reply
      0
      • L Luc Pattyn

        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.

        P Offline
        P Offline
        piul
        wrote on last edited by
        #3

        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;
        			//...
        		}
        
        L 1 Reply Last reply
        0
        • P piul

          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;
          			//...
          		}
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups