Sharing violation (Desktop)

alfcen

Well-Known Member
Licensed User
Longtime User
Situation:
1. download an image via http.dll
2. save the image using the binary.dll and properly close the file
3. draw the saved image on a form
4. download the same image again for a refresh

The newly downloaded image can't be saved with the same name because
Windows throws an error: "The file may be use by another process..."
I would assume that a properly closed file can be overwritten.
Curiously, this does not happen every time (but too often).

Any thoughts are welcome :)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem is between steps 3 and 4.
When you draw an image from a file the file gets locked.
One solution is to first load the image to a Bitmap object and then draw the Bitmap object.
However you can draw the image without saving it at all by using the Door library:
http://www.b4x.com/forum/showpost.php?p=11017&postcount=2
 

alfcen

Well-Known Member
Licensed User
Longtime User
Thanks for your fast response, Erel

Actually, the error happens at FileOpen when I download a second time
and save with the same file name (var LocalFile), irrespective of whether
the image is drawn on a form or not.

Looks like the image file the Writer object creates gets locked when
completely saved.

I was hoping to work around without additional DLLs. The image needs
to be saved for offline viewing.

B4X:
  Response.New1
  Request.New1(URL)
  Request.TimeOut = 60000
  Response.Value = Request.GetResponse
  Reader.New1(Response.GetStream,true)
  FileOpen(c1,LocalFile,cRandom)
  Writer.New1(c1,false)
  Dim buffer(4096) As byte
  count = Reader.ReadBytes(buffer(),4096)
  Do While count > 0
    Writer.WriteBytes2(buffer(),0,count)
    count = Reader.ReadBytes(buffer(),4096)
  Loop
  FileClose(c1)     
  Response.Close
  ' DisplayImage(LocalFile, wf, hf, sf)   'The image is drawn on the form here. Error persists if I REM this line
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code works properly here:
B4X:
Sub App_Start
For i = 1 To 2
    Response.New1
  Request.New1("http://www.b4x.com/index.html")
  Request.TimeOut = 60000
  Response.Value = Request.GetResponse
  Reader.New1(Response.GetStream,true)
  FileOpen(c1,"1.txt",cRandom)
  Writer.New1(c1,false)
  Dim buffer(4096) As byte
  count = Reader.ReadBytes(buffer(),4096)
  Do While count > 0
    Writer.WriteBytes2(buffer(),0,count)
    count = Reader.ReadBytes(buffer(),4096)
  Loop
  FileClose(c1)     
  Response.Close
Next
End Sub
 
Top