锁定文件超时? [英] LockFile with timeout?

查看:32
本文介绍了锁定文件超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LockFile() 没有等待超时,LockFileEx() 也没有.
如果我想等待超时的文件锁定,我该怎么办?

LockFile() does not have waiting timeout, LockFileEx() does not, either.
If I want to wait for file-lock with timeout, how would I go about it?

也就是说,我如何实现以下内容:

That is, how do I implement the following:

DWORD LockFileTimed(HANDLE h,  
    DWORD milli,  
    __in  DWORD dwFileOffsetLow,  
    __in  DWORD dwFileOffsetHigh,  
    __in  DWORD nNumberOfBytesToLockLow,  
    __in  DWORD nNumberOfBytesToLockHigh)  

推荐答案

我理解您使用 LockFileTimed 的意图,因为当文件无法锁定时它不会立即返回,而是重新尝试锁定文件给定的时间.这样对吗?目前还不清楚这是否是您想要的.

I understand your intention of LockFileTimed as it will not return immediately when the file cannot be locked, but retry to lock the file for the given time. Is it right? It's unclear whether this is what you want.

一个简单的实现方法就是编写一个小循环来检查返回码.

A simple way to implement would be just writing a small loop to check the return code.

DWORD timer_end = GetTickCount() + milli;
while (GetTickCount() < timer_end) {
  if (LockFile(...))
    return TRUE;
  else
    Sleep(0); // Wait sometime.
}
return FALSE;

这篇关于锁定文件超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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