Flush data on CF or SD

G

Guest

Hi,

I've created an application that creates its own binary file and for every
modifications to the data I call Flush method of BinaryWriter class.

In this way, if the application crashes, I don't have lost of data.

This is true if the file is on folder like My Documents, but it is false if
the file is on storage card like CF or SD; in this case seems that Flush
doesn't work.
When I try to open the same file after a reset I don't have any data and the
file is empty.

Is this normal ? Is there a way to solve this problem ?

I now that I can close and open the file again but what happens when I have
to do hundred and hundred of changes in the data ?

Thank you in advance.

Keven Corazza
SierraSoft
 
C

Chris Tacke [MVP]

The controller for the external device likely has its own hardware caching
mechanisms to provide optimal speed and media life. The FSD doesn't know
about it, so while the FSD may flush its write cache, the hardware behind it
won't and therefore you don't get a true physical flush. There's really no
way around that.

-Chris
 
G

Guest

Thank you for your answer Chris,

the only way is close the file at every changes ? In this case my code is
more or less the following:

long iPos=FMainStream.Position; // save the current position

FWriter.Close(); // close stream
FMainStream.Close();

// reopen stream
FMainStream=new FileStream(FJobName, FileMode.OpenOrCreate,
FileAccess.ReadWrite);
FWriter=new BinaryWriter(FMainStream);

// restore the previous position
FMainStream.Position=iPos;

Now the question is: because I have thousand of changes and for every
changes I have to close the file and create a new Stream, this situation can
broduce bad effects ?
I mean, create thousand of times the stream classes can produce problem on
memory fragmentation or something like that ?

Thank you again for your help.

Keven Corazza
 
C

Chris Tacke [MVP]

It shouldn't cause fragmentation, but it will likely be slow and could
greatly reduce the life of the media. It's a cost you have to weight
against the need to always flush changes.

-Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top