By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Run code EVERY time something closes

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
"Scott M" <sc*****@nospam.nospam> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
|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
|
Nov 21 '05 #2

P: n/a
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.

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:Oe**************@TK2MSFTNGP09.phx.gbl...
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
"Scott M" <sc*****@nospam.nospam> wrote in message
news:u0**************@TK2MSFTNGP15.phx.gbl...
|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
|


Nov 21 '05 #3

P: n/a
> 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.Exite d, _
AddressOf ProcessExit

Nov 21 '05 #4

P: n/a
No, when I would do
Process.GetProcessesByName("operationsapp")(0).Kil l() the code is not
executed. Would finalize be a good solution for this?

"AMercer" <AM*****@discussions.microsoft.com> wrote in message
news:B8**********************************@microsof t.com...
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.Exite d, _
AddressOf ProcessExit


Nov 21 '05 #5

P: n/a
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/micro.../browse_thread
/thread/6f4ed768dd9573b3/91b5b01483ba041d?lnk=st&q=%22Process.Exited%22+%22 P
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.

Nov 21 '05 #6

P: n/a
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!

""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:xd****************@TK2MSFTNGXA01.phx.gbl...
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/micro.../browse_thread
/thread/6f4ed768dd9573b3/91b5b01483ba041d?lnk=st&q=%22Process.Exited%22+%22 P
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.


Nov 21 '05 #7

P: n/a
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
"Scott M" <sc*****@nospam.nospam> wrote in message
news:e4*************@tk2msftngp13.phx.gbl...
| 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.
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
| message news:Oe**************@TK2MSFTNGP09.phx.gbl...
| > 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
| >
| >
| > "Scott M" <sc*****@nospam.nospam> wrote in message
| > news:u0**************@TK2MSFTNGP15.phx.gbl...
| > |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
| > |
| >
| >
|
Nov 21 '05 #8

P: n/a
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.

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.