How do I wait to delete a file until after the program I started has finished using it?

CodeMonkey

I have been looking for a way to open a file saved to my computer via a Delphi app with its appropriate application. The file is stored in a Varbinary field in a SQL database, and is loaded into a memory stream and then saved via the TMemoryStream's SavetoFile method. What I would like to accomplish is to open the saved file in its appropriate application without knowing the filepath to that application's executable. I have had some success using ShellExecuteEx, but certain applications don't return an HProcess (Windows Live Photo Gallery, for example), so I can't (or at least don't know how to) wait for the application to close before moving on when a handle isn't returned. Is there a way to ensure I receive a handle when calling ShellExecuteEx? If this is not the best way how should I go about doing this?

I only need to know the external app's status because I plan on deleting the file after it closes, and I only need to write it because I'm fairly certain I can't load the file stored in the SQL table into memory (by way of a MemoryStream, FileStream, etc.) and launch its associated program directly from my Delphi app. (I've asked about that separately.)

David Heffernan

Trying to detect that the displaying process has closed is brittle and fraught with problems, as you learnt in your previous question. Often times, it's hard to find the process that is used to view the file, and even if you can, there's no certainty the closing the view of the file will close the process. The process may be used to view other files which the user leaves open. I think the lesson that you should take from that is that the system does not want you to do what you are trying to do.

So, what's the better way to solve the problem? I think the best you can do is to create the temporary files in the temporary directory and not attempt to delete them when the user has finished with them. You could:

  • Remember the files you created and when you create, say the 21st file, delete the first one you made. Then delete the 2nd when you create the 22nd and so on.
  • Or, delete all temporary files on startup. This would remove files from a previous session.
  • Or run a separate tidy up thread that, every ten minutes, say, deleted files that were created more than an hour ago.

You get the idea. The point is that it is an intractable problem to detect when the viewer has finished with the file, in full generality. So you need to think creatively. Find a different way around the road block.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I make an Excel RefreshAll wait to close until finished?

From Dev

How can I wait until an application has started?

From Dev

How can I force this jquery to wait until click event has finished?

From Dev

How can I wait until Asynchronous callbacks are finished before using the data retrieved?

From Dev

In NodeJs how can I wait until my http get is finished?

From Dev

How can I wait until NSXMLParserDelegate methods finished?

From Dev

How to wait until networking by Alamofire has finished?

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

How to do an atomic SFTP file transfer using JSch, so that the file is not accessable until the write process has finished?

From Dev

How do i wait until a file is done downloading

From Dev

How can I know how long a Jenkins job has been in the wait queue after the job is finished?

From Dev

How do I block the current thread until OnComplete has finished executing without the use of traditional threading primitives?

From Dev

How to wait until my batch file is finished

From Dev

How do I detect that a popover has finished?

From Dev

How can I delay an action until after the $apply is finished?

From Dev

Wait until gobalEval has finished

From Dev

Wait until gobalEval has finished

From Dev

I cant do wait my code until a toggle animation in finished in jQuery UI

From Dev

How do I run an additional process after the Apex data load wizard has finished

From Dev

In Spark Streaming (PySpark), how do I stop after it has finished streaming over RDD once?

From Dev

How to make a python Script wait until download has finished

From Dev

How to wait until canvas has finished re-rendering?

From Dev

How can I wait until an external process has completed?

From Dev

How can I call a function after an interval has finished?

From Dev

how do I get my program to close once finished in python

From Dev

How to make Gulp wait until dest file is finished?

From Dev

How do I make a batch file that started a process to wait for the process to terminate?

From Dev

How do I check when my ListView has finished redrawing?

From Dev

How do I make an activity wait until Facebook login is complete?

Related Related

  1. 1

    How do I make an Excel RefreshAll wait to close until finished?

  2. 2

    How can I wait until an application has started?

  3. 3

    How can I force this jquery to wait until click event has finished?

  4. 4

    How can I wait until Asynchronous callbacks are finished before using the data retrieved?

  5. 5

    In NodeJs how can I wait until my http get is finished?

  6. 6

    How can I wait until NSXMLParserDelegate methods finished?

  7. 7

    How to wait until networking by Alamofire has finished?

  8. 8

    Android - How to wait until a SnapshotListener has finished?

  9. 9

    How to do an atomic SFTP file transfer using JSch, so that the file is not accessable until the write process has finished?

  10. 10

    How do i wait until a file is done downloading

  11. 11

    How can I know how long a Jenkins job has been in the wait queue after the job is finished?

  12. 12

    How do I block the current thread until OnComplete has finished executing without the use of traditional threading primitives?

  13. 13

    How to wait until my batch file is finished

  14. 14

    How do I detect that a popover has finished?

  15. 15

    How can I delay an action until after the $apply is finished?

  16. 16

    Wait until gobalEval has finished

  17. 17

    Wait until gobalEval has finished

  18. 18

    I cant do wait my code until a toggle animation in finished in jQuery UI

  19. 19

    How do I run an additional process after the Apex data load wizard has finished

  20. 20

    In Spark Streaming (PySpark), how do I stop after it has finished streaming over RDD once?

  21. 21

    How to make a python Script wait until download has finished

  22. 22

    How to wait until canvas has finished re-rendering?

  23. 23

    How can I wait until an external process has completed?

  24. 24

    How can I call a function after an interval has finished?

  25. 25

    how do I get my program to close once finished in python

  26. 26

    How to make Gulp wait until dest file is finished?

  27. 27

    How do I make a batch file that started a process to wait for the process to terminate?

  28. 28

    How do I check when my ListView has finished redrawing?

  29. 29

    How do I make an activity wait until Facebook login is complete?

HotTag

Archive