Search code examples
c#.netcrashshutdown

Preparing a real-time app to save state data for unforseen events such as unexpected shutdown, crashes, etc


I have an app that conducts an interactive exam session for students on pre-configured computers without an internet connection. The app records user input, system metrics, etc. during the exam and follows an incremental approach to saving this data so that a sudden crash or power outage does not destroy the session data for the entire exam.

The app serializes data every second to a new file with a time stamp and compiles everything together at the end of the session to mark it as an ATOMIC transaction. This works fine.

What I want to do is make the app more robust in responding to unexpected scenarios such as shutdown-initiation triggered by a UPS, the user themselves accidentally kicking the power down button (it happens), etc. Of course I understand that not much can be done if the OS crashes or the power line breaks.

What I am looking for is:

  • a comprehensive list of events to subscribe to such as shutdown initiation, battery level critical for laptops, etc.
  • How to subscribe to these events under .NET 4 (any language).

Solution

  • You can't do nothing with power off. Application.UnhandledException and Close will trap anything you have a vague chance at dealing with which would include a close signal from a soon to be exhausted battery, or a UPS kicking in.