2

I use the c# process class to open a separate application that has no GUI. it is s a c++ project that I use the Process.Kill() on to end it. The problem is this doesn't give it a chance to finish its actions. Process.Close() just releases the object in my main app and and leaves the separate application running.

Whats the best way to make this process close gracefully?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Sam
  • 200
  • 11
  • The class should have a Dispose() method that performs cleanup and closes any objects that needs to be closed (or stop running). – jdweng Dec 05 '16 at 12:59
  • I think you should not use Process.Kill(). Rather send message(udp...) between two applications and when completed its actions kill. – huse.ckr Dec 05 '16 at 12:59
  • [Also some example for async:](http://stackoverflow.com/a/611127/3060520) – huse.ckr Dec 05 '16 at 13:01
  • @jdweng You do realize that what you say has nothing to do with the topic? – Thorsten Dittmar Dec 05 '16 at 13:04
  • 2
    @Sam The most important question here is: Is the C++ project also your project? Do you have access to the source code? If you can modify the tool: there are many options for inter process communication starting with UDP communication over TCP communcation to named pipes. If found it easy in the past to design my tools so I could send commands to them on standard input. Then, in C#, write a "close" command to the standard input of the tool so the tool knows it should quit. – Thorsten Dittmar Dec 05 '16 at 13:05
  • @ThorstenDittmar yes i can add to this c++ project if i wish. Any communication between the 2 projects would be advantageous if the time to develop is not too high. – Sam Dec 05 '16 at 13:08
  • Also to note is that i could have multiple copies of the c++ started by my main app at any given time. Meaning i would only want to close specific instances and not all at once. – Sam Dec 05 '16 at 13:11
  • Note that named pipes are unix specific. Windows Events might also be a suitable mechanism. – Martin Bonner supports Monica Dec 05 '16 at 14:07

1 Answers1

1

In the past I needed to start a non-gui C# tool from a GUI C# tool, both of which I had written. I used the standard input/output streams to communicate between both tools.

You could make your C++ tool listen on its stdin for a command that indicates the tool should quit gracefully. Then, in your C# application, send the quit command, wait for a reasonable amount of time and if the tool hasn't ended by then, kill it.

I'm sorry that my C++ is not good enough to give you an example for the C++ side, but the following lines are used in my C# project to tell the external process to end:

m_consoleProcess.StandardInput.WriteLine("QUIT");
m_consoleProcess.WaitForExit(10000);
if (!m_consoleProcess.HasExited)
{
     m_consoleProcess.Kill();
}

If you need to manage separate external processes, of course you need to store one Process instance for each of them and you have to handle the case that the external process was ended before your application should quit.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139