即使在进程终止时也能保证代码执行

发布于 2024-10-08 03:15:51 字数 176 浏览 3 评论 0原文

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

这可能吗?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梓梦 2024-10-15 03:15:51

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

如果保存状态是应用程序的重要部分,则应该采用类似于数据库系统的方法。实现事务日志、创建检查点等。这是您能得到的最接近的结果。

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

除此之外,这实际上取决于您想做什么。还有你为什么想出这个主意?我们能得到更多细节吗?也许这里有人有更好的选择。

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.

雨夜星沙 2024-10-15 03:15:51

进程被不优雅地终止的全部意义在于它只是停止了正在做的事情。我实在看不出有什么办法可以解决这个问题。

The whole point of a process being killed ungracefully is that it just stops what it is doing. I can't really see a way around that.

我爱人 2024-10-15 03:15:51

您需要考虑为什么您的程序退出。如果是因为错误,那么可以使用try/catch。在 UNIX 术语中,当进程管理器暂停进程时发生的事情是终止(即发送 SIGKILL 信号),它不允许程序在进程退出之前执行任何操作。许多病毒的做法是有两个进程(可能使用共享内存以避免持续的数据同步),每个进程都监视另一个进程的状态,当一个进程出现故障时,另一个进程会重新启动。也许第二个进程可以针对您的情况以类似的方式监视和保存状态。另一种信号是 SIGTERM。当您告诉计算机重新启动但有进程正在运行时,会发送此信号。内核允许程序尝试自行退出,但最终会询问用户是否可以终止该程序。如果您想处理 SIGTERM 查找处理信号。最终,我所知道的 SIGKILL 的唯一解决方案是两个进程解决方案。

You need to think about why your program is exiting. If it is because of an error, then you can use try/catch. In unix terms, what goes on when the process manager halts a process is a kill (i.e. sends a SIGKILL signal) which doesnt allow the program to do anything before the process is exited. What many viruses do is have two processes (possibly with shared memory to avoid constant data synchronization), each monitoring the state of the other and when one goes down, the other respawns it. Perhaps a second process could monitor and save the state in a similar way for your case. The other kind of signal though is a SIGTERM. This signal is sent when you tell your computer to restart but there are processes running. The kernel allows the programs to try and quit on their own, but eventually will ask the user if it is okay to kill the program. If you want to handle SIGTERM lookup handling signals. Ultimately the only solution that I know of to the SIGKILL is the two process solution.

各自安好 2024-10-15 03:15:51

我正在寻找与发布的原因有些不同的原因,但我为自己想出了一个非常好的解决方案,在这里声明可能有用:

注意:对于那些知道它是什么的人来说,这本质上是“心跳”设计模式。

1)除了运行将“死亡”(即被杀死)的代码之外,向该项目添加代码,以便它在初始化时生成一个线程(或可能是另一个方法?),并且该线程所做的就是运行一个never-结束 while 循环(但确保它在迭代之间至少休眠几秒甚至 1 分钟),并在 while 循环中记录一个指示符以指示您的应用程序处于“活动状态”(即“心跳”)。我个人建议在数据库或文件中设置一个值,即当前时间戳(即 DateTime.Now)

2)现在您正在记录“心跳”,创建另一个不执行任何操作的应用程序(即另一个进程)但读取心跳值(即当前时间戳)并在永无休止的 while 循环中执行此操作。一旦确定心跳值是“坏”(即也许,例如,HeartBeatTimestamp + 5 分钟< DateTime.Now),您现在可以运行您希望“一旦进程被终止”运行的代码

希望这有帮助:) 如果您想了解更多信息,请随意从其他资源中研究“心跳”设计模式!

干杯,

杰夫

I was looking around for a somewhat different reason than what is posted, but I came up with a pretty good solution for myself and it might useful to state here:

NOTE: To those that know what it is, this is essentially the 'Heartbeat' design pattern.

1) In addition to the code running that will 'die' (ie be killed), add code to this project such that it spawns a thread (or possibly another method?) upon initialization, and all the thread does is run a never-ending while loop (but make sure it sleeps at least a few seconds or even 1 minute between iterations) and in the while loop, record an indicator to indicate your app is 'alive' (ie the 'heartbeat'). I personally recommend setting a value in either the DB, or perhaps a file, that is the current timestamp (ie DateTime.Now)

2) Now that you are recording the 'heartbeat', create another app (ie another process) that does nothing but reads the heartbeat value (ie the current timestamp) and does this in a never-ending while loop. Once its determined that the heartbeat value is 'bad' (ie maybe if, say, HeartBeatTimestamp + 5 min < DateTime.Now), you can now run your code that you desire to run 'once the process is killed'

Hope this helps :) Feel free to research the 'Heartbeat' design pattern from other resources to if you want to know more!

Cheers,

Jeff

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文