0

I have two C# applications that invoke ScreenScaper.exe; when one exits, I don't want it to end anything called Screenscaper.exe, I want it to end the specific Process ID.

How do I programmatically end an application by its process ID; which also means how do I get the ProcessID when I spawn an application programmatically?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • So you have a MainApplication witch will start some child applications and you want to close them if your MainApplication closes? – Jan Wiesemann Jun 29 '16 at 13:26
  • If your Main Application spawns Screen scrapper processes beneath it, if you are kicking off screenscrapper with the Process.start you could track those in a collection of some kind and before you fully exit the main application loop through and call Process.Kill on all your spawned processes. Main Application would only contain spawned processes its associated with. – Bearcat9425 Jun 29 '16 at 13:30
  • It seems like what you're asking is how to differentiate applications by their ProcessID instead of ProcessName, I edited your question to reflect that. – George Stocker Jun 29 '16 at 15:13
  • Thanks George, your explanation is much more concise. – Connor Shepler Jun 30 '16 at 17:28

1 Answers1

0

CreateToolhelp32Snapshot may help you find parent (==associated?) process ID.

For example let's assume you'd like to kill all "ScreenScraper.exe" processes with no parent process (i.e. those hanged during normal closedown process). The code then may look like this:

class Program
{
    static void Main(string[] args)
    {
        string procName = "ScreenScraper"; // Mind no ".exe" extension here
        foreach (var process in Process.GetProcessesByName(procName))
        {
            try
            {
                var parent = myProcessEx.GetParentProcess(process.Id);
                Console.WriteLine("{0} {1}; PARENT: {2} {3}",
                    process.Id, process.ProcessName, parent.Id, parent.ProcessName);
            }
            catch (ApplicationException exception)
            {
                if (exception.InnerException is ArgumentException)
                {
                    Console.WriteLine("An orphan '{0}' process found with PID {1}. Killing it...",
                        process.Id, process.ProcessName);
                    process.Kill();
                }
                else
                    throw;
            }
        }
    }
}

The myProcessEx class is taken from the sample code section of http://www.pinvoke.net/default.aspx/kernel32.createtoolhelp32snapshot.

Note that a terminated parent process ID may be reused (https://stackoverflow.com/a/185298/4295017) so you probably need to elaborate the parent check code a little bit more.

Community
  • 1
  • 1
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • This looks very promising! i am going to look into the createtoolhelp32snapshot more before proceeding with implementation but thank you! – Connor Shepler Jun 30 '16 at 17:31