Click here to Skip to main content
15,065,324 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Is there a way/process to eliminate the delay when writing a record to a file. I am wirting records to a file but when it is interrupted (unknown cause, user ended it, etc) it will sometimes not write the records to the file.
Posted
Comments
Kornfeld Eliyahu Peter 23-Jul-14 16:15pm
   
Write without buffering...
Sergey Alexandrovich Kryukov 23-Jul-14 17:01pm
   
The question as you formulated it does not really make sense. You need to clarify it.
—SA

"I cannot change the laws of physics!" - Scotty, "The Naked Time" -1966

It takes time to do everything.
Open the file,
load the data (to a buffer) from the disk for the sector(s) containing the location where the new record will be written,
transfer the new record data to the buffer,
write the buffer back to the disk,
close the file (updating the file modification time stamp, disk allocation tables, etc.)

The system can be interrupted at any point above and there's no way to recover.
   
Comments
Sergey Alexandrovich Kryukov 23-Jul-14 17:00pm
   
You are right, but I feel the user means something different, like aborting the operation by other thread or something like that. Perhaps OP tried to make the thread abortable by adding some artificial Thread.Sleep operations, spin wait. I say so because I know some typical mistakes of the developers. With the information OP provided, it's nearly impossible to give a satisfactory answer. We would need some OP's clarification.
—SA
CaptainJack683 24-Jul-14 16:14pm
   
But after program has written 10 or so data records to buffer/file should they not be written on the actual file.
Matt T Heffron 24-Jul-14 17:43pm
   
Not unless you force the buffer to be flushed, as you noted in your "Solution".
The OS is free to keep buffers un-flushed pretty much as long as it wants until told otherwise.
you would use the autoflush = true within streamwriter or streamwriter.flush() after each writeline.

using (StreamWriter outStream = new StreamWriter(fromPath + outFileName + fileSuffix))
{
// set the writing file to be auto-flushed so as each line gets written right away.
outStream.AutoFlush = true;
//
// ... program stream here
//
outStream.WriteLine(strText);
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900