Tara
Tara

Reputation:

How to save backup files for every 10 sec's like VS saves to recover data when power goes off in C#

i would like to save the backup file for every 10 seconds to a particular path,(like VS saves backupfile and asks "Recover files" after opening that file when power goes off) in c# using serialization.

Upvotes: 1

Views: 128

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500385

Add a timer which fires every 10 seconds. Take an in-memory snapshot of the data, and then save it to disk in the background. Make sure that when you save, you save to a new file and only remove the old backup after the new file has been successfully written:

  • Step 1: Before starting to write the new back-up:

    file.backup (old back-up)
    
  • Step 2: Writing the new back-up:

    file.backup (old)
    file.tmp (new)
    
  • Step 3: Rename old back-up:

    file.backup.old (old)
    file.tmp (new)
    
  • Step 4: Rename new backup:

    file.backup.old (old)
    file.backup (new)
    
  • Step 5: Delete old backup:

    file.backup (new)
    

When you recover, you need to take all this into account - in particular work out whether you want to use the old or new backup when you see step 3.

You might want to make this more sophisticated by checking whether anything has actually changed before doing any writes... and possibly consider making the timer slightly longer than 10 seconds, too. I suspect most people don't mind losing a minute's work...

Upvotes: 2

Related Questions