1

I'll use process events to wire up a method that will print something when the console is exited. You can see that because I don't know which event to wire up, I used three that all sound like they would work.

To test this, use the code below. The console window will stay open for 5 seconds. Click the "x" in the top right to close the window prior to the 5 seconds. Expected is that the CurrentDomain_ProcessExit method will be called. It doesn't get called. What is the correct way of doing this using managed .net code? I would rather not use p/invoke etc.

public static void Main(string[] args)
{
    var currentProcess = Process.GetCurrentProcess();
    currentProcess.Exited += CurrentDomain_ProcessExit;
    AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
    AppDomain.CurrentDomain.DomainUnload += CurrentDomain_ProcessExit;

    Thread.Sleep(5000);
}

internal static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
    Console.WriteLine("we just captured the process exit."); // <-- this is never reached.
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
sapbucket
  • 6,795
  • 15
  • 57
  • 94
  • What's the end goal here? If you're able to catch this event, what are you wanting to do, and why? – Brendan Green Oct 12 '18 at 00:06
  • Unfortunately it's not simple. see https://stackoverflow.com/questions/1119841/net-console-application-exit-event – muratgu Oct 12 '18 at 00:10
  • 2
    Possible duplicate of [.NET Console Application Exit Event](https://stackoverflow.com/questions/1119841/net-console-application-exit-event) – 41686d6564 stands w. Palestine Oct 12 '18 at 00:12
  • @Brendan: my parent console window spawns several other console applications (the children). When I kill the parent, I want the children to be killed, too. As far as I can tell there is no hierarchy or internal management to this, therefore I believe I need to kill the children in the above manner. – sapbucket Oct 12 '18 at 00:12
  • @muratgu: surely there must be an easy .Net, managed solution to this? – sapbucket Oct 12 '18 at 00:14
  • @ahmed: not a duplicate: that answer uses non-managed pinvoke style code; I want a managed .net solution. – sapbucket Oct 12 '18 at 00:16
  • @sapbucket: unfortunately no .net solution, since this is a "windows" issue. windows kills the process when you hit ctr-c or close the window. it's not going to let your .net code know before doing it. – muratgu Oct 12 '18 at 00:19
  • @sapbucket AFAICT, there isn't one. Pinvoke is your only option (again, AFAIK) and there's nothing wrong with using it. – 41686d6564 stands w. Palestine Oct 12 '18 at 00:21
  • I would advise looking at the Job Object Windows API, e.g. [here](https://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net). Yes you'll have to use P/Invoke but it is probably the only thing that is guaranteed to work for the scenario you describe, automatically shutting down child processes on any exit condition of the parent. – Mike Zboray Oct 12 '18 at 00:33
  • @everyone: I am surprised by how difficult this is! I will study your responses and perhaps revisit the pinvoke solutions. Moderator, please keep this open for a few days in case a solution appears from someone else. I don't want to completely shut the door on a .Net/managed solution. – sapbucket Oct 12 '18 at 14:32

0 Answers0