Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1054
  • Last Modified:

Adventures with "OleControl Shell.Explorer.2" deleting the temporary files issue.

ok, got my OleControl Shell.Explorer.2 activex control to work and it shows pdfs and graphics files.  But of course it needs a pdf file to exist so I create one.  I am storing the documents in my mssql database as a varchar(max)

So, when my user wants to view the document, I create a temporary file and ole.navigate2 to the file.  

But when I close the form, I want to delete the temporary file but of course it is in use.

So I navigate2 to "About:Blank" but I still have to wait .3 of a second before it is released.

delete file (tempfilename)

Open in new window

Tried a DOEVENTS but no difference.  I also have this after I navigate to the blank page:

DO WHILE (this.Olecontrol1.Busy OR this.Olecontrol1.ReadyState <> 4) 

Open in new window

but again, still need the delay.  So any ideas, not sure if the inkey(.3) is going to be enough for all my client's slow machines and it seems like a patch and not a solution.
  • 3
  • 2
2 Solutions
Yes, the control (or OS) releases the file for deletion after certain delay obviously. I am also navigating to a blank page and waiting for ready state and that's not sufficient in some cases...

So I am attempting to delete the temp file in a loop for 2 seconds:
ldDate = DATE()
lnTime = SECONDS()
llSuccess = .F.
DO WHILE !m.llSuccess AND SECONDS() < m.lnTime + 2 AND DATE() = m.ldDate
  llSuccess = .T.

The advantage of this solution is no delay when the deletion attempt succeeds.
But it still does not ensure the file deletion sometimes. In such case the file waits for temp folder general cleanup which deletes all files older than 1 week from the temp folder when the app exits.
peterhuppAuthor Commented:
Actually I figured this one out and I think it is the best solution.  By using the open I do not attempt the deletion till it either times out or I am able to get RW access.

   lnAttempts= 0
   lnHandle = -1
   DO WHILE lnHandle=-1 and lnAttempts<51 
      lnAttempts = lnAttempts + 1 
      lnHandle = fopen(this.tmpFileName,12)  
   * if timeout handle that otherwise
   DELETE file (this.tmpFileName)

Open in new window

This is a subset of my code but it shows the logic of the fopen to test the release of the file.
Yes, your solution must work because the delay could be more than 5 seconds which is enough even for slow systems.
peterhuppAuthor Commented:
I did try the try..delete file.... catch but sometimes it would make VFP crash.
peterhuppAuthor Commented:
My solution worked the best and 100% of the time.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now