程序关闭后,Windows 中的文件可以锁定多长时间?

发布于 2024-08-26 11:41:36 字数 138 浏览 2 评论 0原文

在我使用的几个脚本中,我遇到了间歇性的问题。
有时,尝试删除文件时脚本会失败。根据错误日志,由于该文件被其他进程访问。我猜测在对文件执行的上一个操作结束后,Windows 没有时间释放该文件。

窗口应该有时间再次释放文件的时间是多少时间?

In a couple of scripts that I use I have problem that is intermittent.
Sometimes the script fails when trying to delete a file. According to the error log due to the file being accessed by an other process. I'm guessing that windows not had time to release the file after the previous operation performed on the file ended.

What amount of time would be a good guesstimate after which windows should have had time to release the file again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-09-02 11:41:36

如果 Windows 应用程序处理完该文件,则应立即关闭它,因为它们可能关闭了文件句柄。文件关闭操作后解锁文件没有时间延迟。

如果程序忘记关闭其文件句柄,但结束后,Windows 将为它们释放它(只是不是立即)。通常它不会很长,但可以是任何时间,我还没有看到它花费的时间超过几秒钟。但应进行适当的清理以避免其被锁定。

还值得一提的是,并非所有程序都以锁定方式打开文件。他们可以打开文件,指定他们想要给予其他进程什么类型的访问权限,并且他们还可以锁定文件的部分内容。他们可以使用其他进程的完全读/写权限打开该文件。

如果您无法控制不关闭其文件句柄的进程,但您需要执行它,则可以编写某种循环来继续尝试该文件几秒钟。

If the Windows app is done working with the file it should be closed instantly, because presumably they closed their file handles. There is no delay in time to unlock a file after a file close operation.

If a program forgets to close their file handles though, but ends, Windows will free it for them (just not instantly). Usually it's not long but it can be any amount of time, I haven't seen it take longer than a couple seconds. But proper cleanup should be done to avoid it being locked.

It's also worth mentioning that not all programs open files in a locked way. They can open file specifying what type of access they'd like to give other processes, and they can also lock portions of the file. They may open the file with full read/write permissions to other processes.

If you have no control over the process that is not closing its file handles, but you need to execute it, you could write some kind of loop to keep trying the file for a few seconds.

孤单情人 2024-09-02 11:41:36

正如另一位用户所发布的,如果文件已正确关闭,则应立即完成 - 无限期延迟,直到操作系统将其解决,否则......

始终,始终正确处置资源。

As another user has posted, it should be done instantly if the file has been closed correctly - with an indetminate delay until the OS sorts it out otherwise...

Always, always dispose of resources correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文