I need to execute a portion of code (the state save) on the process stopping - by itself, by user, by task manager, etc.
Is it possible?
try {} finally {}
, AppDomain.ProcessExit
, IDisposable
, destructor,.. what next to try?
I need to execute a portion of code (the state save) on the process stopping - by itself, by user, by task manager, etc.
Is it possible?
try {} finally {}
, AppDomain.ProcessExit
, IDisposable
, destructor,.. what next to try?
As others have pointed out, there is no way you can execute any code in your application when it is being Killed
by Operating System
or User
. That is why its called Killing
.
If saving the state is an important part of your application, you should take an approach similar to a database system. Implementing transaction log
, creating checkpoints
, etc. That is the closest you can get.
In that case, when your application revives (is re-run after being killed
), it can check these transaction logs
for any pending updates or last state changes.
Other than that, it really depends on what you want to do. And also why you came up with this idea? Can we get more details? May be someone here has better alternative.