如何等待删除文件,直到程序启动完成后才能使用? [英] How do I wait to delete a file until after the program I started has finished using it?

查看:151
本文介绍了如何等待删除文件,直到程序启动完成后才能使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种通过适当的应用程序通过Delphi应用程序打开保存到我的电脑的文件的方法。该文件存储在SQL数据库中的Varbinary字段中,并加载到内存流中,然后通过TMemoryStream的SavetoFile方法进行保存。我想要完成的是在相应的应用程序中打开保存的文件,而不知道该应用程序的可执行文件的文件路径。我已经使用ShellExecuteEx取得了一些成功,但某些应用程序不返回HProcess(例如Windows Live照片库),所以我不能(或至少不知道如何)等待应用程序关闭之前当手柄没有返回时移动。有没有办法确保在调用ShellExecuteEx时收到一个句柄?如果这不是最好的方法,我该怎么办?

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?

我只需要知道外部应用程序的状态,因为我打算在关闭文件后删除文件,我只需要写它,因为我相当确定我无法将存储在SQL表中的文件加载到内存中(通过MemoryStream,FileStream等),并直接从我的Delphi应用程序启动其关联的程序。 (我已分别询问。 / a>)

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

推荐答案

试图检测到显示过程已经关闭是脆弱的,充满了问题,正如你在以前的问题。通常情况下,很难找到用于查看文件的过程,即使可以,也不能确定关闭文件的视图将关闭该过程。该过程可以用于查看用户离开打开的其他文件。我认为你应该从中得到的教训是系统不希望你做你想做的事情。

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:


  • 记住您创建的文件,创建时,说第21个文件,删除您制作的第一个文件。然后在创建第22个时删除第2个等等。

  • 或者,在启动时删除所有临时文件。这将从前一个会话中删除文件。

  • 或者运行一个单独的整理线程,每十分钟一次,删除超过一小时之前创建的文件。

你得到这个想法。关键是,检测完成该文件的时间是完全一般的,这是一个棘手的问题。所以你需要创造性思考。在路障中找到不同的方法。

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.

这篇关于如何等待删除文件,直到程序启动完成后才能使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆