Click to See Complete Forum and Search --> : How to wait until a file is written to disk


June 3rd, 1999, 10:13 AM
I'm using a workerthread wich waits until a change occurs in a directory
then I check if it is the filetype that this thread should process.
the only problem is that this thread tries to open the file before the file is written completly to disc

this is the code I'm using at the moment

//variables
WIN32_FIND_DATA FoundThis;

lpszFileToFind = "*.dat";
HWND hWnd = (HWND) pParam;
HANDLE hChange = FindFirstChangeNotification("d:\\MyProgram\Read,
FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME
);

while (m_bExit == FALSE) // global variable
{ if(WaitForSingleObject(hChange, 20) == WAIT_OBJECT_0)
{
if (FindFirstFile(lpszFileToFind, &FoundThis) != INVALID_HANDLE_VALUE)
{
PostMessage(hWnd,WM_USER_START,0,0);
}
}
}




the routine reads files before that they are fully writen to disk by another program is there option to stop this.
Or am I completly wrong and is another part of my program to blame.

Ravi Bhavnani
June 3rd, 1999, 11:10 AM
One way to approach this is to open the file for exclusive access. If the open fails (but the file exists), just wait for a while and retry.

/ravi

June 4th, 1999, 01:49 AM
Thanks