|
-
December 16th, 2002, 02:35 AM
#1
help on monitor the folder
Hi, guru,
I'm a VC beginner and now building a FTP client.
I want to build a programme to monitor the folder like "d:\temp". If a new file is created on the folder, the ftp it to the server.
My question is:
what event or function I need to check there is a new file created on the folder?
rgds,
-
December 16th, 2002, 03:33 AM
#2
Look out for FindFirstChangeNotification(), FindNextChangeNotification(), FindCloseChangeNotification() and WaitForSingleObject().
-
December 17th, 2002, 02:35 AM
#3
the question in programme
Hi, friend,
Thanks for the reply.
I look up some sample of MS and try to use it. But there is a dead loop.
I build the function:
OnButtonClick()
{
function1();
function2();
}
The function1() is OK. For function2():
void function2()
{
DWORD dwWaitStatus;
HANDLE dwChangeHandles;
// Watch the m_strMonitorFolderName directory for file creation
dwChangeHandles = FindFirstChangeNotification(
m_strMonitorFolderName, // directory to watch
FALSE, // do not watch the subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes
if (dwChangeHandles == INVALID_HANDLE_VALUE)
ExitProcess(GetLastError());
while (TRUE)
{
// Wait for notification.
dwWaitStatus = WaitForSingleObject( dwChangeHandles, 0);
if(dwWaitStatus==WAIT_OBJECT_0)
{
function3();
if ( FindNextChangeNotification( dwChangeHandles) == FALSE )
ExitProcess(GetLastError());
}
}
Question:
1. Because I want the programme monitor the folder after strat up, so I use while(true) in function2. But there seems the programme is dead lock after call function2.
2. I want to monitor whether there is a new file created, is that right to use
dwChangeHandles = FindFirstChangeNotification(
m_strMonitorFolderName, // directory to watch
FALSE, // do not watch the subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes
3. I want to programme react immediatly, so whether it is right to use
dwWaitStatus = WaitForSingleObject( dwChangeHandles, 0);
cheers!
-
December 17th, 2002, 03:32 AM
#4
Normally, when WaitForSingleObject() is being used, the current thread will be suspend until timeout or the event occurs. So, to prevent the program from being frozen, it is quite common to spawn-off a thread for the monitoring purpose. This is the reason why your program dead lock.
I am not sure if FILE_NOTIFY_CHANGE_FILE_NAME works but I had tried FILE_NOTIFY_CHANGE_LAST_WRITE before.
Code:
AfxBeginThread(NotifyThreadProc, 0)
UINT NotifyThreadProc(LPVOID pParam)
{
BOOL bKill = FALSE;
HANDLE hMonitor = NULL;
// First time for tracking change notification.
hMonitor = FindFirstChangeNotification(path, FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE );
while(!bKill)
{
//
// Wait for the change notification.
//
DWORD dwWait = WaitForSingleObject(hMonitor, INFINITE);
// Handles the return result.
switch(dwWait)
{
case WAIT_OBJECT_0:
// Detected a change in the folder.
// Do something.
break;
case WAIT_ABANDONED:
// Perform error handling.
break;
case WAIT_TIMEOUT:
// Perform error handling.
break;
case WAIT_FAILED:
// Perform error handling.
break;
default:
break;
}
// Subsequent change notification tracking.
FindNextChangeNotification(hMonitor );
}
return 0;
}
-
December 19th, 2002, 02:36 AM
#5
Really Thanks!
Cheers! I know a lot because I'm a beginer of VC.
-
December 19th, 2002, 04:57 AM
#6
-
January 15th, 2003, 10:08 AM
#7
hi
i found that thread interesting but i need some advice now
it is possible to know the file that notify me?
i want to know what file was opened from a folder and when
can you help me regarding this problem?
thank you in advance for your help
-
January 15th, 2003, 07:35 PM
#8
ReadDirectoryChangesW().
I had never use this API before but I believe this is what you are looking for. Please tell me if it works. Thanks.
-
January 15th, 2003, 07:43 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|