Flush file to disk

Just starting out? Need help? Post your questions and find answers here.
miskox
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Flush file to disk

Post by miskox »

Hello all!

I create .png file with CreateImage and at the end I use SaveImage.

Program generates few hundred files in less than a second but then I have to wait for the files to be flushed to disk.

FlushFileBuffers can be used for files but not for images.

I want the program to end when everything is written to disk.

Advise please.

Thanks.
Saso
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Flush file to disk

Post by RASHAD »

Maybe
Edit :
I am confused
You saved the images to where ?
Better to post a simple runnable snippet

1- Read the Image file as DATA(ReadData(#File, *MemoryBuffer, LengthToRead)`
2- Write the DATA (WriteData(#File, *MemoryBuffer, Length) to disk as file
3- FlushFileBuffers(#File)
Egypt my love
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Flush file to disk

Post by boddhi »

Hello,
miskox wrote: I want the program to end when everything is written to disk.
If you don't use threads, SaveImage() returns control to the program when it has completed the operation.

So, once the last SaveImage() operation has been performed, the program can be closed.
 
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Bitblazer
Enthusiast
Enthusiast
Posts: 761
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Flush file to disk

Post by Bitblazer »

RASHAD wrote: Thu Jul 04, 2024 9:54 pm 1- Read the Image file as DATA(ReadData(#File, *MemoryBuffer, LengthToRead)`
2- Write the DATA (WriteData(#File, *MemoryBuffer, Length) to disk as file
3- FlushFileBuffers(#File)
Indeed - see for example file caching on windows and SetSystemFileCacheSize
To flush the cache, specify (SIZE_T) -1
IMHO - Don't flush the buffers after each file, or your app might get super slow for some users. If you flush the buffers once your application closes, it should be ok. But the best might be to not flush the buffers at all and leave it to the operating system.
miskox
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Flush file to disk

Post by miskox »

Thank you all.

I will use RASHAD's solution.

And to answer RASHAD's question:
You saved the images to where ?
FIles are saved to a folder. Program closes when all .png files are created (files are created in a second (not each file - all of them)) and then I have to wait for the OS to write/flush these files to disk. (so if I do

Code: Select all

c:\dir *.png
)

I see them appearing.

Thanks again.

Saso
BarryG
Addict
Addict
Posts: 4122
Joined: Thu Apr 18, 2019 8:17 am

Re: Flush file to disk

Post by BarryG »

miskox wrote: Fri Jul 05, 2024 4:31 pmI see them appearing
See them appearing: how? From an Explorer folder window? That's just Windows taking a moment to show it, but the file is already there until the folder view refreshes. I hope this isn't the cause of your whole problem. I write files to disk without flushing and they're there INSTANTLY, despite Windows not showing them for a moment.
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Flush file to disk

Post by AZJIO »

miskox wrote: Fri Jul 05, 2024 4:31 pm then I have to wait for the OS to write/flush these files to disk.
There is a difference in the files on the disk and in how they are displayed by the Explorer program. If you copy files to a large folder, then the explorer, in addition to adding the file to the end of the list, also sorts the list. The more files, the more time it takes to sort. In addition, the explorer reacts to changes in the file system, that is, you copy not to the explorer window, but simply to the disk, while the explorer receives an event about the file system change, it checks which folders are open in the explorer windows and begins to update the folders in which changes occur . At the same time, I don’t know if there is a delay for sorting, that is, if you copy a lot of files into a folder, then theoretically it is beneficial for the explorer to wait until you copy all the files or wait with a slight delay in order to sort the files copied in the last 3 seconds to the receiver. If the explorer sorts 3 thousand files after copying one file, then you must understand that after copying 10 files, the explorer will sort 3 thousand files 10 times and will not have time to redraw the window after new files appear. Let's repeat the result: the files have already been copied, and the explorer continues to process callback events and draw files in the window.
miskox
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Flush file to disk

Post by miskox »

Thank you all. It turns out I was too quick. When I ran the program from the IDE I thought that it was already finished but it was not. I am using Total Commander and not Windows Explorer so pressing Refresh slowly showed the files (didn't notice the red X that the program was still executing).

Sorry for all the troubles.
Saso
miskox
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Flush file to disk

Post by miskox »

Update:

I did some more checks:

Test code:

Code: Select all

EnableExplicit
Define.i counter
UsePNGImageEncoder()
For counter = 1 To 200
  If CreateImage(0, 1180, 1650, 24, #White)
    SaveImage(0,RSet(Str(counter),3,"0")+".png",#PB_ImagePlugin_PNG)
  EndIf
  Next counter
End
In compiler options: Windows executable
if I run this .exe from a command prompt it finishes in a fraction of a second. Then execute dir *.png from the same command prompt and you will see that files are being created.

Change Windows executable to Console and program finishes when files are created on disk.

Also: If I run Windows/Console version from a batch file program finishes when files are actually written to disk.

Thank you all.
Saso
Post Reply