1

Imagine launching a process or start an application, however in task manager makes a kill this process. Is there an event associated?

This code, cancel the user close the window application

private void Form1_Aplicacao_FormClosing(object sender, FormClosingEventArgs e)
{
   if (e.CloseReason == CloseReason.UserClosing)
      e.Cancel = false;
}

But if I using process.Kill() method, FormClosing event has not been fired. My question: Have an event to get a kill this own process?

Eriksson
  • 159
  • 1
  • 2
  • 11
  • Possible duplicate of http://stackoverflow.com/questions/3326242/c-sharp-windows-form-killed-by-task-manager-is-there-a-way-to-run-shutdown-fun – arunes Feb 22 '12 at 14:53
  • I do not believe that you will find an event when an application is terminated abruptly in that fashion. – Grant H. Feb 22 '12 at 14:53

3 Answers3

3

No event is raised when a process is killed. Most likely, when killed the process is simply removed from the scheduler's list of runnable tasks, its memory is deallocated, and then the OS picks up any remaining pieces afterwards. I would definitely not count on even destructors or garbage collection running, so any resources which are in use that the OS cannot clean up will be leaked.

(Actually) killing a process means that you slay it, slice it up, grind it, and toss the pieces into the ocean. More technically, no jump into the killed process' code is issued, so there can be no event fired inside that code. Remember that events, like a lot of other flow-control constructs (loops, conditionals, etc) as well as multitasking itself, are just fancy gotos. Very useful fancy gotos, admittedly, but still simply jumps to different parts of available code.

user
  • 6,897
  • 8
  • 43
  • 79
1

Short answer: no, there is no way for your application to be aware (for instance with an event that is raised) that its hosting process has been brutally killed.

ken2k
  • 48,145
  • 10
  • 116
  • 176
0

It's not quit right. You can use process communication Technics such as PIPES.

You can send throw PIPES and wait for answer. You can create a rule that if answer hasn't been received in X time, the process has been killed.

But this in just one of the process communication options, for more information you can check in the following link: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

Also look at the following option: C# process.start, how do I know if the process ended?

Community
  • 1
  • 1
Oras
  • 120
  • 1
  • 9