Sharing violation (Desktop)

alfcen

Well-Known Member
Licensed 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 :)
 

alfcen

Well-Known Member
Licensed 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

Administrator
Staff member
Licensed User
The following code works properly here:
B4X:
Sub App_Start
For i = 1 To 2
    Response.New1
  Request.New1("http://www.basic4ppc.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
 

Erel

Administrator
Staff member
Licensed User
It works fine (on the desktop).
When I use this file with 'Form1.Image = ThisFile' I get the access violation error.
 
Top