如何确保在崩溃时保存应用程序状态 C#
我对 C# 编程非常陌生,因此请记住:
我有一个内存中的数据对象,其中包含当我的应用程序崩溃或关闭时我需要保存信息的数据。有没有办法确定或可靠地做到这一点?
我一直在研究析构函数
~MyObjectName(){}
终结器和 Dispose(),
但据我了解,这些都不能可靠地实现我想要的功能?
目前我正在使用析构函数,当我关闭程序时它可以工作,但这并不意味着它会在崩溃时或始终工作。
我也应该关注事件吗?
I am very new to c# programming, so that in mind:
I have an in-memory data object with data I need to save the information when(if) my application were to crash OR closed. Is there a way to do this deterministically or reliably?
I have been looking at destructors
~MyObjectName(){}
finalizers and Dispose(),
but as far as I understand none of these will do reliably what I want?
Currently I am using the destructor, and it works when I am closing the program, but this doesn't mean it will work on crashing, or always.
Should I be looking at events as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当进程(任何进程,不仅仅是 .Net 进程)终止时,没有 100% 可靠的机制可以用来保存数据(或执行其他任何操作) - 大多数进程都可以使用任务管理器中的“结束进程”选项随时终止,当发生这种情况时,进程将立即被终止。作为一个更极端的例子,电源线可以从机器背面拉出。
如果此数据对象不是 100% 需要保持最新并在进程终止后保存,则 AppDomain.UnhandledException 事件 可能就足够了。
如果这种情况绝对 100% 有必要,那么您需要在流程运行时不断保存此信息 - 绝对不能保证您以后有机会这样做。这就是数据库的运行方式,直到某些更改记录以某种格式(例如事务日志)记录到磁盘上时才会返回事务。这就是 ACID 中 D 的含义。
There is no 100% reliable mechanism that you can use to save data (or do anything else for that matter) when a process (any process, not just a .Net process) terminates - most processes can be terminated at any point using the "End Process" option in the task manager, when this happens the process is immediately killed. As a more extreme example the power cord could be pulled out the back of the machine.
If its not 100% necessary that this data object be up-to-date and saved once the process is killed then the AppDomain.UnhandledException Event may suffice.
If its absolutely 100% necessary that this be the case then you need to be continuously saving this information as the process is running - there is absolutely no guarentee that you will get a chance to do it at a later date. This is how databases operate, no transaction returns until some record of the change has been recorded to disk in some format (e.g. a transaction log). This is what the D stands for in ACID.
我相信您正在寻找捕获未处理的异常?像这样的东西:
I believe you are looking for catching unhandled exceptions? something like this:
您可以使用windbg 来实现这一点。
You can achieve this with windbg.