Run code EVERY time something closes

  • Thread starter Thread starter Scott M
  • Start date Start date
S

Scott M

I have a procedure in my Dispose method that records my application was
closed in a database. However, when I kill a process or an unhandled
exception occurs this does not seem to run. Is there some code that is
GUARANTEED to run EVERY time the code exits? I don't mind if it runs during
garbage collection or whatever. I just need it to run to log the
application closing for compliance reasons.

Thanks in advance
Scott
 
Scott,
Is this a Windows Forms app?

If so:

Have you looked at using the Application.ApplicationExit event?

In addition to the Application.ThreadException &
AppDomain.UnhandledException events?

The ApplicationExit event should let you know when your app is exiting
normally, while the ThreadException will prevent exceptions during "window
event processing" from killing your app, and UnhandledException will let you
know when your app is not exiting normally.

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net


|I have a procedure in my Dispose method that records my application was
| closed in a database. However, when I kill a process or an unhandled
| exception occurs this does not seem to run. Is there some code that is
| GUARANTEED to run EVERY time the code exits? I don't mind if it runs
during
| garbage collection or whatever. I just need it to run to log the
| application closing for compliance reasons.
|
| Thanks in advance
| Scott
|
 
Yes, it is a windows form. How about if the process is killed? I really
need the code to run every time it exits. No matter how it exits.
 
Yes, it is a windows form. How about if the process is killed? I really
need the code to run every time it exits. No matter how it exits.

I think you want what is shown below, but I don't claim to be an authority.
I guess that all gui objects are not usable when Exited is raised.

Private Sub ProcessExit(ByVal sender As Object, ByVal e As EventArgs)
' current process Exited event handler
End Sub

' wire up process exited event handler in main or somewhere similar
AddHandler System.Diagnostics.Process.GetCurrentProcess.Exited, _
AddressOf ProcessExit
 
No, when I would do
Process.GetProcessesByName("operationsapp")(0).Kill() the code is not
executed. Would finalize be a good solution for this?
 
Hi

For unhandled exception, you may try Jay's suggestion.
But for Process.Kill, it will call the API TerminateProcess directly, which
did not give the process to have chance to fire that events. That is why we
did not suggest call terminate the process forcely because that will cause
many problems.
You may try to take a look at the link below.
http://groups.google.com/group/microsoft.public.dotnet.general/browse_thread
/thread/6f4ed768dd9573b3/91b5b01483ba041d?lnk=st&q=%22Process.Exited%22+%22P
rocess.Kill%22&rnum=1&hl=zh-CN#91b5b01483ba041d

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
So, my best bet is to make some sort of an API that will close the
application from another process or have the process terminate itself....
Humm. Okay. I'll have to re-write the code that I pounded out today.
Thanks much for the info!
 
Scott,
As Peter suggests.

If another app terminates your app. It is terminated immediately without any
chance for any more code to run.

I vaguely remember (very vaguely) there was a Win32 "callback" that your app
could use to allow it a "last chance" to avoid being terminated, however I
don't have a reference handy.

--
Hope this helps
Jay [MVP - Outlook]
T.S. Bradley - http://www.tsbradley.net


| Yes, it is a windows form. How about if the process is killed? I really
| need the code to run every time it exits. No matter how it exits.
|
| message | > Scott,
| > Is this a Windows Forms app?
| >
| > If so:
| >
| > Have you looked at using the Application.ApplicationExit event?
| >
| > In addition to the Application.ThreadException &
| > AppDomain.UnhandledException events?
| >
| > The ApplicationExit event should let you know when your app is exiting
| > normally, while the ThreadException will prevent exceptions during
"window
| > event processing" from killing your app, and UnhandledException will let
| > you
| > know when your app is not exiting normally.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > |I have a procedure in my Dispose method that records my application was
| > | closed in a database. However, when I kill a process or an unhandled
| > | exception occurs this does not seem to run. Is there some code that
is
| > | GUARANTEED to run EVERY time the code exits? I don't mind if it runs
| > during
| > | garbage collection or whatever. I just need it to run to log the
| > | application closing for compliance reasons.
| > |
| > | Thanks in advance
| > | Scott
| > |
| >
| >
|
 
Hi Scott,

You are welcomed.
For your scenario, I have the ideas as follows.
1. The best approach is to use the try catch blocks or
Application.ThreadException & AppDomain.UnhandledException events as Jay's
suggestion?
2. If you use the Kill approach(including using the taskmamager), the
process will terminate immedidately and the code to maintain the
exceptional exit is not executed. That is to say it is hard to gurantee
the maintaining code running if other terminate the process with kill
approach.
3. A possible idea to set a flag in the file/registry when the process has
run the terminate code and check it in the next start. If that one is not
set, which means it may be killed, even someone poweroff the PC suddenly,
we need to check something according to our program logic.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top