即使在进程终止时也能保证代码执行 [英] Guarantee code execution even on process kill

查看:21
本文介绍了即使在进程终止时也能保证代码执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在进程停止时执行一部分代码(状态保存) - 由自身、用户、任务管理器等执行.

I need to execute a portion of code (the state save) on the process stopping - by itself, by user, by task manager, etc.

有可能吗?

try {} finally {}AppDomain.ProcessExitIDisposable、析构函数、..接下来要尝试什么?

try {} finally {}, AppDomain.ProcessExit, IDisposable, destructor,.. what next to try?

推荐答案

正如其他人所指出的,当应用程序被 杀死 时,您无法执行应用程序中的任何代码>操作系统或用户.这就是为什么它被称为Killing.

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.

如果保存状态是您的应用程序的重要组成部分,您应该采用类似于数据库系统的方法.实现事务日志,创建checkpoints等,这是你能得到的最接近的.

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.

在这种情况下,当您的应用程序恢复(在 killed 后重新运行)时,它可以检查这些 事务日志 是否有任何挂起的更新或最后的状态更改.

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.

这篇关于即使在进程终止时也能保证代码执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆