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

后端 未结 2 698
野性不改
野性不改 2021-01-07 12:54

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 databas

2条回答
  •  悲&欢浪女
    2021-01-07 13:24

    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.

提交回复
热议问题