1

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)  
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Andrei
  • 8,606
  • 10
  • 35
  • 43

1 Answers1

0

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;
minjang
  • 8,860
  • 9
  • 42
  • 61