Sharing violation (Desktop)

Discussion in 'Questions (Windows Mobile)' started by alfcen, Apr 15, 2008.

  1. alfcen

    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 :)
     
  2. Erel

    Erel Administrator Staff Member Licensed 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.basic4ppc.com/forum/showpost.php?p=11017&postcount=2
     
  3. alfcen

    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.

    Code:
    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(4096As 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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The following code works properly here:
    Code:
    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(4096As 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
     
  5. alfcen

    alfcen Well-Known Member Licensed User

  6. Erel

    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.
     
  7. alfcen

    alfcen Well-Known Member Licensed User

    Thanks a lot for testing, Erel :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice