Overwrite file after dispose

Joosttt

Member
Licensed User
Hi,
I'm having a problem overwriting a previously downloaded image after I disposed the image. What I want to do is download an image and show it on the main form more than one time. But the second time I download the image and want to overwrite the old one I get an error saying:
"The process cannot access the file '(downloaded file)' because it is being used by another process."
It is obviously still being used by the application and can therefore not be overwritten. But I did dispose the image. Is there a way to force the application to release the image so it can be overwritten?

A workaround would be to give the image another name each time it is being downloaded. But that way I would end up with a directory full of unused images.

To clarify my problem I wrote a little app that I will attach.


Regards,
Joost
 

Attachments

  • test.sbp
    1.7 KB · Views: 217
Last edited:

agraham

Expert
Licensed User
Longtime User
To avoid this sort of thing you need to manage your bitmaps directly rather than trying to have them implicitly managed by disposing things using them. Note that bitmaps are the only things you need to worry about managing and disposing, otherwise you should just let the .NET grabage collector do its thing. The problem in this case is that until the image bitmap is explicitly disposed it is holding its source file open and preventing its reuse and that won't happen until the .NET garbage collector runs which it won't for a while as your little application is not using up managed memory very fast.

You should download my http://www.b4x.com/forum/additional-libraries/3171-imagelibex-library.html and read the help topic "Bitmap memory management".

Test2.sbp attached has a crude modification using a BitmapEx from ImageLibEx to do what you want to do.
 

Attachments

  • test2.sbp
    1.9 KB · Views: 210

Joosttt

Member
Licensed User
Thanks Agraham. I already have your ImageLibEx library and ran across the 'Bitmap memory management' before but skipped it. I will look it up again and actually read it this time :).

:sign0012: hehe
 
Top