Cleaning up after the process is ended in task manager

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a problem that relates to running "cleanup" code when an application
is forcibly ended using the "End Process" of "Task Manager" (Please note that
this is very different from "End Task" since end task sends a message to the
application, requesting it to close)

My real problem is that our application makes entries into a database when
the application starts, and then corresponding entries when the application
closes ("cleanup"). This application is run in a terminal session, and
disconnected sessions are forced closed after a certain amount of time has
passed, effectively ending all the processes of the terminal session. This is
equivalent to "End Process" of the application using Task Manager.

I'm unable to run any code when this happens, the code does not return after
the Application.Run(...) statement, so putting a try-catch-finally around it
doesn't work because it isn't run.

Adding event handlers to AppDomain.ProcessExit doesn't work, nor does adding
a MessageFilter to the Application's message loop.

How do I get code to run under these circumstances ?
 
Hi Jaans,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to write clean up code even
the process is ended by TerminateProcess. If there is any misunderstanding,
please feel free to let me know.

As far as I know, this cannot be achieved. TerminateProcess is the
low-level process killing function. It bypasses DLL_PROCESS_DETACH and
anything else in the process. Once you kill with TerminateProcess, no more
user-mode code will run in that process. It's gone. For more information,
please check the following link:

http://weblogs.asp.net/oldnewthing/archive/2004/07/22/191123.aspx

In this case, I think you can do the clean up when process starts. You can
set a flag to see if the process is exit properly. If not, check it at the
beginning of your app, and do clean up.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Kevin

Yes your understanding is correct. Thank you for confirming what I suspected
might not be possible. The link also was helpfull.

This made me think about Terminal Server in particular. Is it issuing a
"TerminateProcess" to the processes running in a sessions when it has reached
it's disconnected timeout, or is it sending closing the applications some
other way?

We have done some tests (remote debugging), is it's behaviour is similar to
that experienced by "TerminateProcess".

Why I'm so interested in cleaning up, is that a GUID ticket is issed as a
form of "license control" when the application starts, which needs to be
"returned" when the application closes. I guess I'm going to have to change
it to issue tickets that "expire" or somthing like that.

Regards
 
Hi Jaans,

As far as I know, it is issuing a TerminateProcess. If so, I think you can
try to set timeout to let the ticket expire after a period of time.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Jaans,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top