I am working in a project involving data acquisition. One very important requisite is described like this:
- At the beginning of the recording, a file must be created, and its headers must be written;
- As soon as the data-acquisition starts, the application should keep saving collected data to file periodically (typically once per second);
- Writing consist on appending data blocks to the file, atomically if possible;
- Should any error occur (program error, power failure), the file must be kept valid untill the last write before the error.
So I plan to use some Thread to watch for data received and write this data do file, but I don't know which practice is best (code below is not real, just to get the feeling):
First option: Single open
using (var fs = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
fs.Write(headers, 0, headers.Length);
using (var fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
{
while (acquisitionRunning)
{
Thread.Sleep(100);
if (getNewData(out _someData;))
{
fs.Write(_someData, 0, _someData.Length);
}
}
}
Second option: multiple open:
using (var fs = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
fs.Write(headers, 0, headers.Length);
while (acquisitionRunning)
{
Thread.Sleep(100);
if (getNewData(out _someData;))
{
using (var fs = new FileStream(filename, FileMode.Append, FileAccess.Write))
{
fs.Write(_someData, 0, _someData.Length);
}
}
}
The application is supposed to work in a client machine, and file access by other processes should not be a concern. What I am most concerned about is:
- Do multiple open/close impact performance (mind that typical write interval is once per second);
- Which one is best to keep file integrity safe in the event of failure (including explicitly power failure)?
- Is any of this forms considered a particularly good or bad practice, or either can be used depending on specifics of the problem at hand?