如何确保在崩溃时保存应用程序状态 C#

发布于 2025-01-06 04:00:21 字数 284 浏览 0 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(3

清风挽心 2025-01-13 04:00:21

当进程(任何进程,不仅仅是 .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.

空城旧梦 2025-01-13 04:00:21

我相信您正在寻找捕获未处理的异常?像这样的东西:

static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);

  Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

  Application.Run(new Form1());
}

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
  MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
  // here you can log the exception ...
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
  // here you can log the exception ...
}

这个例子展示了如何管理所有尚未发生的异常
在 try-catch 部分中捕获(在 Windows 窗体应用程序中)。

UnhandledException 事件处理从以下位置抛出的未捕获的异常
主 UI 线程。 ThreadException 事件处理未捕获
从非 UI 线程引发的异常。

I believe you are looking for catching unhandled exceptions? something like this:

static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);

  Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

  Application.Run(new Form1());
}

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
  MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
  // here you can log the exception ...
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
  // here you can log the exception ...
}

This example shows how to manage all exceptions that haven't been
caught in the try-catch sections (in Windows Forms application).

The UnhandledException event handles uncaught exceptions thrown from
the main UI thread. The ThreadException event handles uncaught
exceptions thrown from non-UI threads.

婴鹅 2025-01-13 04:00:21

您可以使用windbg 来实现这一点。

  1. 保留 断点。当您的应用程序退出时将调用此方法。
  2. 当到达断点时,使用 !dumpheap -type MyObjectName 来获取对象的地址
  3. 使用 !dumpobject "address of MyObjectName" 来了解对象内部的值

You can achieve this with windbg.

  1. Keep a breakpoint in zwterminateprocess method in windbg. This method will be called when your application exits.
  2. when the breakpoint is reached , use !dumpheap -type MyObjectName to get the address of your object
  3. Use !dumpobject "address of MyObjectName" to know the values inside the object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文