I need a way to detect when my application is terminated either through the Task Manger or by shutting down Windows, so I can run some code. The FormClosing event does not seem to catch these. Is there a way that I can do this? Thanks...
Printable View
I need a way to detect when my application is terminated either through the Task Manger or by shutting down Windows, so I can run some code. The FormClosing event does not seem to catch these. Is there a way that I can do this? Thanks...
No and for good reason. If you're application terminated abnormally then something is very wrong either with the system or the application. Running additional code would not be useful.
You should employ a good exception handling scheme with logging. Beyond that if your application is terminating (i.e. no exception is being thrown) then there is nothing you can do.
The only option if you really wanted to do this (you shouldn't) would be to write a server or separate application to monitor the first application.
double click "My Project" in the solution explorer, and on the "Application" tab, click the "View Application Events" button (near the bottom, next to the splash screen combo box)
Once in the code view, select "MyApplication Events" from the left dropdown, and then "Shutdown" from the right combo.
Any code you put there should fire when the app is terminated. Note that I don't believe it will fire in the case of an "End Process" in the taskmanager if you are in the processes tab, but it does work when clicking "end task" from the applications tab in the task manager.
I didn't test, but the code should also fire fine when windows is being shutdown or the profile is being logged off.
Thanks kleinma ... I will give that a try.
I don't really agree with everything kasracer says. I mean yes of course have good exception handling in your app, but I don't really think this is an issue of exceptions, this is an issue of you wanting to know when your app is shutting down.
It sounds like you were originally handling this in the form closing event to do this until you found it didnt fire in certain scenarios, so that is why you were looking for somewhere else to put the code to fire for these fringe scenarios.
Although do keep in mind that if the app is being terminated because of a shutdown or endtask, then windows does sort of give a timelimit on how long the app has to respond and exit, before windows will force a termination. What that means is try to keep whatever code in the shutdown event to a minimum, so that you can be pretty confident it will run everytime and not timeout.
So for example writing to the event log, or to your apps own log file of some sort would be very fast, and should not cause any issues. Something like connecting to a remote database and writing data may be beyond the threshold of what you should be doing in that event.
This will only work in the event of a shutdown, not a termination. This should trigger, I believe, at the start of a shutdown but the OS can still send a termination to your application.Quote:
Originally Posted by kleinma
When your application is terminated it's done. This event won't fire if it's terminated.
What do you mean? If an application is terminated it's done. No more events or code will be run from that application and there is no way to handle this from within the application.Quote:
Originally Posted by kleinma
If an exception is thrown, then the OP can handle it but if someone terminates the application from task manager then there is no way to know or directly recover without a helper application.
A shutdown you'll get a brief amount of time. An end task, end process, end process tree (basically the same as ending the task) or a terminate signal will kill the application giving you no events or additional code.Quote:
Originally Posted by kleinma
Look, I try to test things before I go and say them because I like to be sure. If I am not sure, I like to be clear that what I am saying is what I think, and it needs to be fully tested to confirm.
You said:
Did you test that? Because I did test it. and it DOES work when you perform an "End Task" in the task manager. The Shutdown event most certainly DOES fire in this scenario. I also stated I did not believe it would work the same for and end process, but it would need to be tested.Quote:
A shutdown you'll get a brief amount of time. An end task, end process, end process tree (basically the same as ending the task) or a terminate signal will kill the application giving you no events or additional code.
You also said this:
I totally agree, but again I don't think he was talking about when his application is TERMINIATED, I think he is talking about when his application is TERMINATING (I know he said terminated, but I also know how to read between the lines sometimes). There is a big difference, as one is after the fact, and one can be handled while its happening to perform some additiona task real quick before it shuts down.Quote:
What do you mean? If an application is terminated it's done. No more events or code will be run from that application and there is no way to handle this from within the application.
I also don't know why you keep talking about exceptions. Where did the OP mention anything about exceptions? He just asked how to do something when the app is shutting down, and nothing more...
It will fire during shutdown but there is no guarantee any code you run will finish executing. Vista (and XP to some extent) will end the process if it doesn't close quickly enough.Quote:
Originally Posted by kleinma
As far as "End Task" is concerned, everything I saw said it basically did an End Process Tree as it kills the main process and its children. After looking into it further, you're right it does trigger that event but it works like shutdown in that it can easily be terminated so, again, you may not get enough time to run your code depending on what it's doing.
During an end task or shutdown, I'm not so sure I'd want to open any handles to the file system or a database as an interrupted write or update could cause corruption (not as likely on the database side).
He never said shutting down, he said terminated. The way it's worded, it sounded to me like his application is ending due to an error on his system. I mentioned exceptions to illustrate how he could catch and handle them (in case that's what he is experiencing).Quote:
Originally Posted by kleinma
Since the OP didn't give much information, I made multiple assumptions like everyone else had to. Hopefully someone made the right assumption.