MS 的解釋
就是說,Windows 是一個 multi-tasking 的 OS,所以有人跟你同時在 access 一個 file,而且又同時 write....(有點看圖說故事的感覺)
所以有這個error 是很正常的事。
MS提供一個 sample code:
#define MAXRETRIES 5
#define RETRYDELAY 250
HANDLE hFile = INVALID_HANDLE_VALUE
DWORD dwRetries = 0;
BOOL bSuccess = FALSE;
DWORD dwErr = 0;
do
{
hFile = CreateFile( szFile,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if ( INVALID_HANDLE_VALUE == hFile )
{
dwErr = GetLastError();
if ( ERROR_SHARING_VIOLATION == dwErr )
{
dwRetries += 1;
Sleep(RETRYDELAY);
continue;
}
else
{
// An error occurred.
break;
}
}
bSuccess = TRUE;
break;
} while ( dwRetries < MAXRETRIES );
if ( bSuccess )
{
// You succeeded in opening the file.
}
else
{
// Failure occurs. Do graceful error handling.
// Here, you must notify the user of the failure.
MessageBox( NULL,
"Tried to update data file but it was already in use",
"Update error...",
MB_OK | MB_ICONSTOP );
// You also want to put the software back in the state it was in
// on entrance of the current function, as if the user had never
// tried to do the update.
// This may also require deallocating any resources that were
// allocated because of this operation.
}
就是有這個 error 的時候,就 sleep 一下,等那個好心的程式write 結束,再 試一次。
Windows XP 還有話說,因為一堆不知名的程式在background 跑 (virus scan, source control.. speed hd.. etc)
但是 Windows CE 還有這樣的error 真是.. 見鬼了
沒有留言:
張貼留言