none
Problems with binary files at power failure: Windows 10 IoT Enterprise, code VB.NET, Visual Studio 2017 RRS feed

  • Question

  • Hi,

    I have problems with power failure for a machine. I save data to binary files, even having a backup file with the same data.

    The situation is that when I run the programme I can see that the data.stn contains values. Then I shut it down, and sometimes it works fine but then sometimes it tells me that the binary stream is out of order. When I check with file explorer both files data.stn and BUdata.stn are there but only the binary header is in the file, rest has no data. This means that the programme will not work.  

            StringFile = "C:\Users\Public\data.stn"
            BUStringFile = "C:\Users\Public\BUdata.stn"
            Dim f As New FileStream(StringFile, IO.FileMode.Create)
            Dim fo As New Runtime.Serialization.Formatters.Binary.BinaryFormatter

            Try
                fo.Serialize(f, Me.newMainData.Data)

                …...

                f.Flush()

                f.Close()

            Catch ex As Exception
                f.Close()
                EventLog("Exception when saving file data.stn." & ex.ToString)
            End Try

                ' Make a BackUp copy of the indata file. To be used in case the
                ' original file gets corrupt. The same procedure for BUdata.stn repeated.

    First of all, I read that I had to disable windows writecache policy as it requires a constant access to power. From the beginning I only had f.close() but added f.flush() as I found that this shall write the data directly to the disk without going via any cache!? And both these actions did make the frequency of error to go down. I may also add that I produce other files in the same manner, sometimes they get corrupt, the corrupt files move around aimlessly and not repeatedly. 

    (This is a development of the programme that originally was written for Windows XP Embedded and there we have not experienced any problems of this kind. Frequent power failures but always came up again.)

    The hard disk also have a 60 ms delay from when the power failure occurs that should be enough for the flush to work. But obviously it doesn't. Further I can't understand why both files are empty after the restart?

    What do I do wrong? Help would be most welcome.

    Best Regards,

    Per

    Wednesday, November 13, 2019 2:37 PM

All replies

  • The hard disk also have a 60 ms delay from when the power failure occurs that should be enough for the flush to work. But obviously it doesn't. Further I can't understand why both files are empty after the restart?

    What do I do wrong? Help would be most welcome.

    Hi

    'What do I do wrong' - probably relying on making a backup AFTER losing power - why not make the backup at a time(s) when the power is good - such as after data has changed, at time of application exit, on a timed schedule, on  a button click.

    You say that 60ms 'should' be enough - well, perhaps it isn't!


    Regards Les, Livingston, Scotland



    • Edited by leshay Wednesday, November 13, 2019 3:43 PM
    Wednesday, November 13, 2019 3:40 PM
  • Hi Les,

    Okey, but the data changes all the time when the user makes input. Thus a backup is no longer correct. If I just wished to make a backup when I have power it would require that the files wasn't changing all the time. The intention of a backup is that when I wish to read from the file again, and the first file gets corrupt then I wish to read from that backup instead. But for some reason BOTH files get corrupt at the power failure. And next time there are 2 other files that gets corrupt. The key is that the files saves input data from the user. So waiting is no point. They have to be updated at the same time.

    Best Regards, Per


    Wednesday, November 13, 2019 3:58 PM
  • Hi

    If the data is written to a db/file/whatever at times through normal use tokeep the data as current as possible, then. write also to the backup. That way, your backup will be as up to data as possible.

    An unusable backup is worthless, a backup which is lackinf a few of the most recent updates in the data is worth a LOT by comparison.

    Relying on a system to survive a HDD write operation without power and without corrupting the entire HDD is a highway to failure.


    Regards Les, Livingston, Scotland

    Wednesday, November 13, 2019 4:07 PM
  • Thanks Les,

    Of course, I agree with what you are stating.

    The funny thing is that this problem was handled in the previous version with exactly the same code but then it was Windows XP embedded. Now it is Windows 10 IoT Enterprise.

    It seems that both files now is in a cache for a period of time and then both gets corrupt. Windows 10 seems to do things different than before, by emptying files that is in a cache when power failure.

    I will try a solution in which I will use a delegate that makes backup at regular time intervals.

    Best wishes, Per

    Thursday, November 14, 2019 8:15 AM