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 frequent enough, so no matter what, in case on 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 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 records number 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.
Michael Haephrati, born in 1964, an entrepreneur, inventor and a musician. Haephrati worked on many ventures starting from HarmonySoft, designing Rashumon, the first Graphical Multi-lingual word processor for Amiga computer.
Worked with Amdocs
and managed several software projects, among them one for the Ministry of Tourism in New Zealand.
During 1995-1996 he worked as a Contractor with Apple
at Cupertino. After returning to Israel, worked as a Project Manager with Top Image Systems
(mostly with JCC
, Nicosia), and then at a research institute made the fist steps developing the credit scoring field in Israel. He founded Target Scoring and developed a credit scoring system named ThiS, based on geographical statistical data, participating VISA CAL, Isracard, Bank Leumi and Bank Discount (Target Scoring, being the VP Business Development of a large Israeli institute).
During 2000, he founded Target Eye, and developed the first remote PC surveillance and monitoring system, named Target Eye.
Other ventures included: Data Cleansing
(as part of the DataTune
system which was implemented in many organizations.