Solved

ReadDirectoryChangesW asynchronous - how to trigger?

Posted on 2009-07-15
7
1,281 Views
Last Modified: 2012-06-27
I'm using Borland C++ and API's ReadDirectoryChangesW in the asynchronous way, to watch a directory for changes. I watch for rename, add delete files or folders within the directory. I hav a loop in my thread like the code you will find below. All works nice so far. .. to nice perhaps ...

The program runs for hours and all my file operations i.e deleting or programaticaly renaming hundrets of files in a split if secons handled in "section #1" correctly.

Hmm... but how to test the code in "section #2" if an asynchronous operation never happens?

My question therefore is only: How to simulate an asynchronous operation to test my code? Or better, how to trigger one?


Thanks in advance :-)
bResult = ReadDirectoryChangesW( ...);
 
if (bResult) { 
 
	// section #1
	blah ...;
	blah ...;
 
}
else {
 
	// section #2
	// code to handel the results of 
	// asynchronous operations
	
}

Open in new window

0
Comment
Question by:hasnpapa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 24862432
How are you using the 'asynchronous' way? Are you using an 'OVERLAPPED' struct or a completion routine?
0
 
LVL 6

Accepted Solution

by:
Ravi Kalla earned 400 total points
ID: 24862531
Step1: Create 2 files "file1" and "file2"
Step2: Write a program to rename a file from "file1" if it is "file11" and "file11" if it is "file1" and put this operation in an infinite loop.
Step3: Write another program to rename a file from "file2" if it is "file22" and "file22" if it is "file2" and put this operation in an infinite loop.
Step4: Now, execute the two programs created in "Step2" and "Step3" along with the program that you have to test. This is an asynchronous operation.
0
 

Author Comment

by:hasnpapa
ID: 24863289
@Ravi:
Realy thanks a lot for this explanation. At this point I don't kwno what a asynchronous operation basicly is. Now I do :-))

@jkr:
I'm using 'OVERLAPPED' struct in "section #2" as shown in the code above :-)

	
else {
	msg = "Reaching section #2!!!!!!!!!!!!!!!";
	Synchronize(&UpdateMemo);
	DWORD dwResult = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
	switch ( dwResult ) {
	case WAIT_OBJECT_0: // then "terminate event"
		this->Terminate();
		break;
	case WAIT_OBJECT_0 + 1: // overlapped operation finished
		bResult = GetOverlappedResult(hDir, &ov, &BytesReturned, FALSE);
		if (!bResult) { // error!
		}
		else { //results of asynchronous operation
		}
	}
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 24863340
There seems to be a misconception - the finishing of the overlapped operation is always the end of an asynchronous action...
0
 

Author Comment

by:hasnpapa
ID: 24863576
Hmm... but i works so far :-)

But, perhaps I have completely misunderstood the asynchronous data workflow ...

Would you have a look at my code please, jkr? I have mixed it up as I think, from the meager examples found tho this topic in forums and websites.

For me it is very important that I fully understand the code that I write and not to only cut&paste something and hope it will work. Do you have any further suggestions for me? Any help to learn this API stuff will be highliy appreciated ...
void __fastcall TShellChangeNotifier::Execute()
{
	HANDLE hEvents[2];
	OVERLAPPED ov;
	memset(&ov, 0, sizeof(ov));
	BOOL bResult;
	DWORD dwError;
	DWORD BytesReturned;
	size_t nBufSize = 32*1024;
	FILE_NOTIFY_INFORMATION* pBuffer = (FILE_NOTIFY_INFORMATION*)calloc(1, nBufSize);
	FILE_NOTIFY_INFORMATION* pBufferCurrent;
 
	ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	hEvents[0] = ov.hEvent;
	hEvents[1] = Form2->m_StopEvent; // stop thread event
 
	HANDLE hDir = CreateFile(MonitorDirectory, FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED, NULL);
 
	while (!Terminated) {
		bResult = ReadDirectoryChangesW(hDir, pBuffer, nBufSize, TRUE, FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_FILE_NAME, &BytesReturned, &ov, NULL);
		if (bResult) {
			DWORD dwResult = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
			if (dwResult == WAIT_OBJECT_0) {
				pBufferCurrent = pBuffer;
				do {
					AnsiString filename = AnsiString(pBufferCurrent->FileName).SubString(0, pBufferCurrent->FileNameLength / 2);
					if (pBufferCurrent->Action == FILE_ACTION_ADDED) {
						msg += "Add: "; msg += filename;
					}
					else if (pBufferCurrent->Action == FILE_ACTION_REMOVED) {
						msg += "Removed: "; msg += filename;
					}
					else if (pBufferCurrent->Action == FILE_ACTION_MODIFIED) {
						msg += "Modified: "; msg += filename;
					}
					else if (pBufferCurrent->Action == FILE_ACTION_RENAMED_OLD_NAME) {
						msg = "Old-Name: "; msg += filename;
						Synchronize(&UpdateMemo);
						pBufferCurrent = (FILE_NOTIFY_INFORMATION*)(((DWORD)pBufferCurrent) + pBufferCurrent->NextEntryOffset);
						msg = "New-Name: ";
						msg += AnsiString(pBufferCurrent->FileName).SubString(0, pBufferCurrent->FileNameLength / 2);
						Synchronize(&UpdateMemo);
					}
					//else if (pBufferCurrent->Action == FILE_ACTION_RENAMED_NEW_NAME) {
					//	msg += "NEW-Name: ";
					//	msg += filename;
					//}
					if (pBufferCurrent->NextEntryOffset) {
						pBufferCurrent = (FILE_NOTIFY_INFORMATION*)(((DWORD)pBufferCurrent) + pBufferCurrent->NextEntryOffset);
						msg += " ... Walk on Buffer";
						Synchronize(&UpdateMemo);
					}
					else {
						pBufferCurrent = NULL;
					}
				} while (pBufferCurrent != NULL);
			}
			else if (dwResult == WAIT_OBJECT_0 + 1) {  the "terminat event"
				this->Terminate();
			}
		}
	}
	else {
		msg = "Reaching section #2!!!!!!!!!!!!!!!";
		Synchronize(&UpdateMemo);
		DWORD dwResult = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
		switch ( dwResult ) {
			case WAIT_OBJECT_0: // the "terminate event"
				this->Terminate();
				break;
			case WAIT_OBJECT_0 + 1: // overlapped operation finished
				bResult = GetOverlappedResult(hDir, &ov, &BytesReturned, FALSE);
				if (!bResult) { // error!
				}
			else { //results of asynchronous operation
			}
		}
	}		
	CloseHandle( hDir );
	free(pBuffer);
}

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 24863779
Let me put it like that - there is no reason to use an OVERLAPPED struct in your code at all, you could just leave it out. You have a single thread of execution, and waiting for the event to be signalled right after calling that function makes little sense. It would be less CPU consuming to wait for a handle created by 'FindFirstChangeNotification()' (http://msdn.microsoft.com/en-us/library/aa364417.aspx) and then call 'ReadDirectoryChangesW()', e.g. like
    HANDLE  hNotify;
    DWORD   dwResult;
 
    hNotify =   FindFirstChangeNotification     (   MonitorDirectory,
                                                    TRUE,
                                                    FILE_NOTIFY_CHANGE_SIZE
                                                );
 
    hEvents[0] = hNotify;
    hEvents[1] = Form2->m_StopEvent; // stop thread event
 
    HANDLE hDir = CreateFile(
                          MonitorDirectory,                   // pointer to the file name
                          FILE_LIST_DIRECTORY,                // access (read-write) mode
                          FILE_SHARE_READ|FILE_SHARE_DELETE,  // share mode
                          NULL,                               // security descriptor
                          OPEN_EXISTING,                      // how to create
                          FILE_FLAG_BACKUP_SEMANTICS,         // file attributes
                          NULL                                // file with attributes to copy
                        );
 
    TCHAR acBuf[1024];
 
    DWORD dwRet;
 
    for (   ;;)
        {
            DWORD dwResult = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
 
 
            ReadDirectoryChangesW(hDir,(LPVOID) acBuf,1024,FALSE,FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_CREATION,&dwRet,NULL,NULL);
 
            FILE_NOTIFY_INFORMATION* pInfo = (FILE_NOTIFY_INFORMATION*) acBuf;
 
            //...
 
            FindNextChangeNotification  (   hNotify);
        };
 
    CloseHandle(hDir);

Open in new window

0
 

Author Comment

by:hasnpapa
ID: 24864223
In my very first atempt, I had a short code like the showen by you, using the synchronous way.

But soon I figured out, that ReadDirectoryChangesW blocks the Termination of the thread and so the Close() of the whole Programm, while it is waiting for the next event.

Well, I cut&paste ;-) your code and compile it.
Now, as the same effect as im my first atempt, the thread cannot be terminated on command, because the ReadDirectoryChangesW is waiting for its event. If I let ReadDirectoryChangesW work asynchronously ... yes, then it will be "terminatable".

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question