0

I have the following code to identify whether a process has exited or not. When it exits I am creating a notification in my application.

Process[] processes = RunningInstancesByName("MyService");

while (processes.Length <= 0)
{
  //Thread.Sleep(100);
  processes = RunningInstancesByName(name);
  }
  Process process = processes[0];

  process.EnableRaisingEvents = true;
  process.Exited += this.Installer_Process_Exited;
  AddExitEvents(name, eventHandler);

  return process;
}

How do I identify whether this exit was triggered by a Windows shutdown or restart? Because if it is so, I should not create a notification through my application? Is there a way to identify whether this process exited because of such shut down or restart events? Please advice.

AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • Maybe [this](https://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionended.aspx) helps –  Sep 08 '17 at 01:12
  • 1
    There is no generic way to determine why a process exited. If it is your own process, you could have it set a specific exit code if it is doing so in response to a system shutdown. Otherwise, I think all you can do is to listen for a shutdown event yourself, and assume that processes that exit after that point are doing so because of the shutdown. (You might also need to ignore process exits that occur shortly *before* you receive a shutdown notification.) – Harry Johnston Sep 08 '17 at 01:44

0 Answers0