.netcompact-frameworkwindows-cehandheld

Windows restoring old value to regedit when abrupt shutdown of the system


I have a program running on a Windows CE 6 (handheld) and I noticed this happening oftenly. I store on HKEY_CURRENT_USER\SOFTWARE\my-app a value which is used to control the last date the user used the app. This value is updated many times inside my program. In a controlled test, I checked the value before removing the battery to force restart and I saw a value there, say "A" for simplicity. But this test failed and I had to restart again. Then, I checked the registry value and now it was value "B". Then, I removed again the battery to make my test and after restart I noticed that value "A" was back again stored on the registry.

I suppose that what is happening is that Windows is not "committing" the value to the permanent storage and that it was buffered in memory when I restarted. Anyway, is there a way to force this value to be stored to the hard drive? Or, if it's not the case, then why is this old value restored back to the registry and how to avoid this to happen? Thanks in advance.


Solution

  • Problem solved. According to msdn, the flush() method solves it:

    It is not necessary to call Flush to write out changes to a key. Registry changes are flushed to disk when the registry uses its lazy flusher. Lazy flushing occurs automatically and regularly after a system-specified time interval. Registry changes are also flushed to disk at system shutdown.

    Unlike Close, the Flush function returns only when all the data has been written to the registry.

    The Flush function might also write out parts of or all of the other keys. Calling this function excessively can have a negative effect on an application's performance.

    An application should only call Flush if it must be absolute certain that registry changes are recorded to disk. In general, Flush rarely, if ever, need be used.

    The thing is that I wasn't giving time enough to this "lazy flusher" to store the registers into the disk. I put a call to flush() right after the SetValue() and after that I noticed that the values was correctly stored and the error I was having stopped.