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

  • Thread starter Thread starter Per Söderberg
  • Start date Start date
P

Per Söderberg

Guest
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

Continue reading...
 
Back
Top