How to save the current status of your work frequently enough, so that if an abnormal termination occurs, you can make your software continue from where it left off.
Introduction
Many types of applications must have the ability to recover from an abnormal termination and continue from where they stopped. Among these applications, hard drive recovery, backup applications, data processing, etc., abnormal termination might be a computer reset, a crash or a human error. This tip is a conceptual and a generic tip, which basically says: save the current status of your work frequently enough, so no matter what, in case of an abnormal termination, you can make your software continue from where it stopped. This tip can be helpful in most cases. I have used it for many years, and it saved me a lot of time, especially with data processing applications, which processed 100,000+ records. It is true that one can always think of scenarios where a specific record or task will cause an unhandled error and the mechanism I am suggesting will recreate the same error again and again, but that can be solved with a proper error handling mechanism.
There are two parts of a simple recovery mechanism:
- Constantly save the current state.
- Recover from an abnormal termination.
Saving the Current State
In order to save the current state, we need to determine which information is sufficient to be able to continue an ongoing process. For example, if we go over a database and process each record, it would be sufficient to save the database details, table / recordset name and an index pointing to the current record processed. There is no need to save the data processed.
Where to Save the Current State
The best place to save such information would be the Registry. You create a set of keys for your application, and save the necessary data as part of the processing loop. Storing such data in the Registry is much faster than opening a data file and storing the data in such file.
Dim LastProcessedRecord As Integer
LastProcessedRecord = CurrentProcessedRecord
My.Computer.Registry.SetValue "HKEY_CURRENT_USER\Software\MyApp", "LastProcessedRecord",
How to Distinguish Between a Normal and an Abnormal Termination
There is no point of having such a mechanism, unless we are able to activate the recovery mechanism only in cases of abnormal termination. To do so, we need to distinguish between these and the normal termination or completion of the task. To achieve that, we need to clear the "last processed record" key (or whatever name is used for it) whenever the task is completed.
The Automatic Recovery
The idea is to automatically detect an abnormal termination last time the application ran, and in such case to pop-up a message to the user, asking him or her whether to continue from where the processing stopped last time. This message will not appear when the application is run for the first time, or when it completes its tasks as expected.
Dim LastProcessedRecord As Integer
LastProcessedRecord = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\
MyApp", "LastProcessedRecord", -1)
When automatic recovery is requested, the initial index is promoted to where it was last time, instead of starting from the first value assigned to it. So if the task involves processing record numbers 100 to 200, and last time the application crashed after reaching record 150, then next time, the index will start with 150 instead of 100.
History
- 27th November, 2011: Initial version