Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Visual Basic

Continuing From Where You Last Stopped

Rate me:
Please Sign up or sign in to vote.
4.81/5 (28 votes)
15 Feb 2013CPOL3 min read 44.7K   16   25
Frequently save current status of work so that you can continue the next time from where you left off
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:

  1. Constantly save the current state.
  2. 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.

VB
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.

VB
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Secured Globe, Inc.
United States United States
Michael Haephrati is a music composer, an inventor and an expert specializes in software development and information security, who has built a unique perspective which combines technology and the end user experience. He is the author of a the book Learning C++ , which teaches C++ 20, and was published in August 2022.

He is the CEO of Secured Globe, Inc., and also active at Stack Overflow.

Read our Corporate blog or read my Personal blog.





Comments and Discussions

 
GeneralMy vote of 5 Pin
_Vitor Garcia_26-Feb-13 4:53
_Vitor Garcia_26-Feb-13 4:53 
AnswerRe: My vote of 5 Pin
Michael Haephrati21-Sep-17 8:59
professionalMichael Haephrati21-Sep-17 8:59 
GeneralGreat and Useful article Pin
Bandi Ramesh16-Feb-13 6:50
Bandi Ramesh16-Feb-13 6:50 
GeneralMy vote of 5 Pin
Joezer BH28-Jan-13 21:15
professionalJoezer BH28-Jan-13 21:15 
GeneralMy vote of 5 Pin
Ruth Aanie24-Jan-13 19:33
Ruth Aanie24-Jan-13 19:33 
GeneralMy vote of 5 Pin
Jason44413-Oct-12 21:49
Jason44413-Oct-12 21:49 
GeneralMy vote of 5 Pin
Barb Henry 213-Oct-12 11:27
Barb Henry 213-Oct-12 11:27 
GeneralMy vote of 5 Pin
George Rogers II13-Oct-12 6:44
George Rogers II13-Oct-12 6:44 
GeneralMy vote of 5 Pin
Emma20123217-Sep-12 6:49
Emma20123217-Sep-12 6:49 
AnswerRe: My vote of 5 Pin
Michael Haephrati21-Sep-17 8:53
professionalMichael Haephrati21-Sep-17 8:53 
GeneralMy vote of 5 Pin
Matan joice17-Sep-12 3:37
Matan joice17-Sep-12 3:37 
AnswerRe: My vote of 5 Pin
Michael Haephrati21-Sep-17 8:59
professionalMichael Haephrati21-Sep-17 8:59 
GeneralReason for my vote of 2 Not descriptive and misleading Pin
Tolga Birdal27-Dec-11 1:27
Tolga Birdal27-Dec-11 1:27 
GeneralRe: Could you be more specific? I will be more than happy to ame... Pin
Michael Haephrati27-Dec-11 1:30
professionalMichael Haephrati27-Dec-11 1:30 
AnswerRe: Reason for my vote of 2Not descriptive and misleading Pin
Michael Haephrati21-Sep-17 9:04
professionalMichael Haephrati21-Sep-17 9:04 
GeneralReason for my vote of 4 Thanks for sharing the concept... it... Pin
_DanO_6-Dec-11 4:06
_DanO_6-Dec-11 4:06 
AnswerRe: Reason for my vote of 4Thanks for sharing the concept... it... Pin
Michael Haephrati21-Sep-17 9:04
professionalMichael Haephrati21-Sep-17 9:04 
GeneralReason for my vote of 3 good tip Pin
sudhansu_k1235-Dec-11 19:21
sudhansu_k1235-Dec-11 19:21 
AnswerRe: Reason for my vote of 3good tip Pin
Michael Haephrati21-Sep-17 9:05
professionalMichael Haephrati21-Sep-17 9:05 
GeneralVery nice article. Please help us further with some sample s... Pin
sudhansu_k1235-Dec-11 19:20
sudhansu_k1235-Dec-11 19:20 
AnswerRe: Very nice article. Please help us further with some sample s... Pin
Michael Haephrati21-Sep-17 9:05
professionalMichael Haephrati21-Sep-17 9:05 
GeneralI agree with Reiss. You really only discuss how to write th... Pin
Kschuler28-Nov-11 6:35
Kschuler28-Nov-11 6:35 
GeneralRe: The title is not misleading. This is a conceptual and a gene... Pin
Michael Haephrati28-Nov-11 7:12
professionalMichael Haephrati28-Nov-11 7:12 
GeneralYour title is misleading - your tip only illustrates how to ... Pin
Reiss27-Nov-11 23:32
professionalReiss27-Nov-11 23:32 
GeneralThanks man i was in so much need of this. hope it works for ... Pin
Jαved27-Nov-11 19:13
professionalJαved27-Nov-11 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.