Friday, 16. May 2008
As part of the project I am doing at the moment, we want to automatically test to ensure that the update works and can handle errors. This involves automatically uploading a test image to the board and running it overnight.
The problem we needed to overcome was how do we stop the device from running the update as soon as part of the file has been put on thus failing the update.
I delved into the wonders of the Win32 API. The good thing about Window CE6 is that most of the functionality that is available on Windows XP etc is available on Windows Embedded.
I found “FindFirstChangeNotification(LPCTSTR lpPathName, BOOL bWatchSubtree, DWORD dwNotifyFilter).”
FindFirstChangeNotification can watch for a number of different events all listed here:
http://msdn.microsoft.com/en-us/library/aa364417(VS.85).aspx
There is no magic “Notify when file handle is closed” which would of been excellent. So I had to hack a little solution to my problems.
This is my simple hacky solution:
HANDLE hChange;
DWORD dwWaitStatus;
//Setup to fire when the file has been written too
hChange = FindFirstChangeNotification("\\NAND", FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE);
//Wait for the first chunk of data to be written
WaitForSingleObject(hChange, INFINITE);
//Loop around until we can be sure that there is no more data being written to the device
do
{
FindNextChangeNotification(hChange);
//Set timeout for a minute so we can be sure there is no more data being
//Written to the device
dwWaitStatus = WaitForSingleObject(hChange, ONE_MINUTE);
}while(dwStatus == WAIT_OBJECT_0);
/*
.... Rest of update code here ....
*/
FindCloseChangeNotification(hChange);
I thought this would be a good bit of information to have incase I ever have to watch a directory for changes again, especially if the file is being transferred over some medium and we need to know some indication that the transfer is over.
Of course this doesn’t check for completeness since the connection could drop out but that would require alot more effort than we have to create.
Cheers,
Alasdair