I admit that I DO NOT understand what this command is really doing.
Somewhere I saw something like the following sequence.
file.Close();
file.Dispose();
VolumeInfo.GetVolumes()[0].FlushAll();
If Disposed what is being flushed?
Or could I just ask…
(Not saying that the above is not correct)
What is the correct sequence or when to use VolumeInfo.GetVolumes()[0].FlushAll()
Thanks in advance
This closes and flushes [em]some time [/em]in the future, it may take many seconds. FlushAll flushes immediately, or at least I never it doing otherwise.
1 Like
When you flush or close a file, you are writing stream buffers (which exist in RAM) to volume buffers (which also exist only in RAM). Those blocks will get written to the physical media at some indeterminate later time. When you flush a volume, you are forcing an immediate transfer of any ‘dirty’ block buffers from RAM to the SD card’s flash.
File systems usually use two or more layers of buffering. the file.Close() will ensure that any outstanding changes in the file stream are written to the block buffers for the Volume, but those are also still in memory. Typically, after some time delay, the SD Volume block buffers will get written to the physical device. The delay is there to lessen the stress on the SD card in case there are more writes coming soon. The VolumeInfo.GetVolumes()[0].FlushAll forces an immediate write of any dirty volume buffers from main memory onto the physical card.
The reason for doing a volume flush is because you, the programmer, KNOW that no more writes are coming, and you want to eliminate the possibility that someone will eject the card before you can write the dirty blocks to the device.
Note that file streams are not the only things that ‘dirty’ volume blocks. Changes to the directory structure (create a file, delete a file, rename a file, resize a file) can also create dirty blocks and forcing a volume flush makes sure that your physical SD media is up to date in case the power fails or someone physically ejects the media.
[Edit: and yes, you have the correct sequence - flush files first, volumes last]
2 Likes
@ mcalsyn -
Thanks for the detailed answer.
(which also exist only in RAM)
Now… Why did I not think of that?
Thanks again
Added: This goes into my notebook
1 Like