Solved

multiple notifications from FindFirstChangeNotification

Posted on 2003-10-25
6
2,092 Views
Last Modified: 2013-12-03
hello,

i am using FindFirstChangeNotification and listening for FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE
on multiple directories.

      DWORD word;
    // Wait for notification.
 
    dwWaitStatus = MsgWaitForMultipleObjects((DWORD)numberofhandles, ChangeHandles,
        FALSE, INFINITE,QS_POSTMESSAGE);

when a message comes in

 do {      //wait and see if we get multiple of the same message and skip them we  
                   only want to run once
      FindNextChangeNotification(ChangeHandles[dwWaitStatus]);
       word = WaitForSingleObject(ChangeHandles[dwWaitStatus], 1000);
        
   } while(word == dwWaitStatus);


The problem i am having is when i trigger a last write by adding to a file and saving it i think i am getting multiple different  notifications.
my little do while loop catches them if its the same message   . what other message am i recieving and how to i make it so i only hand the last write message.

thanks
0
Comment
Question by:tryonix
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9620467
What application are you using to add something to a file? It might well be that a backup copy of the file is created. Have you tried using 'ReadDirectoryChangesW()' to find out what happens?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9620477
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9620505
Well, I'm not sure I understand, but I'd like to point a small mistake here

    dwWaitStatus = MsgWaitForMultipleObjects((DWORD)numberofhandles, ChangeHandles,
        FALSE, INFINITE,QS_POSTMESSAGE);

In case something "happens" in one of the directories, let's assume it's the fifth handle in the array, then the return value will be : WAIT_OBJECT_0 + 4. It could be (I honestly don't know) that WAIT_OBJECT_0 == 0, but you should not assume that.

Now whether or not WAIT_OBJECT_0 is 0, the following code isn't correct :

do {     //wait and see if we get multiple of the same message and skip them we  
                   only want to run once
      FindNextChangeNotification(ChangeHandles[dwWaitStatus]);
       word = WaitForSingleObject(ChangeHandles[dwWaitStatus], 1000);
       
   } while(word == dwWaitStatus);

because
- ChangeHandles[dwWaitStatus] is possibly out of bounds
- WaitForSingleObject() will return WAIT_OBJECT_0 in case an event happened, which will always be different than WAIT_OBJECT_0 + 4 and/or 4

So, I'd replace that line with

   } while(word == WAIT_OBJECT_0);

and make sure that dwWaitStatus has the proper value.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

Author Comment

by:tryonix
ID: 9621447
okay i am a little confused with this method
here is the code i have
can u tell me what i am doing wrong?

while (running)
{
      DWORD word;
    // Wait for notification.
 
    dwWaitStatus = MsgWaitForMultipleObjects((DWORD)numberofhandles, ChangeHandles,
        FALSE, INFINITE,QS_POSTMESSAGE);
      if (dwWaitStatus == i)
      {
            MSG msg ;

        // Read all of the messages in this next loop,
        // removing each message as we read it.
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            // If it's a quit message, we're out of here.
            if (msg.message == WM_QUIT)  
                  {
                              running =false; //end loop
                  }
            else// Otherwise, dispatch the message.
            {
                        DispatchMessage(&msg);
                  }
        } // End of PeekMessage while loop.

      }
      else
      {

   do {      //wait and see if we get multiple of the same message and skip them we only want to run once
      FindNextChangeNotification(ChangeHandles[dwWaitStatus]);
       word = WaitForSingleObject(ChangeHandles[dwWaitStatus], 1000);
        
   } while(word == dwWaitStatus);

//now do what i want
0
 

Author Comment

by:tryonix
ID: 9621481
made a mistake in last post  with

if (dwWaitStatus == i)

should be this

      if (dwWaitStatus == numberofhandles)
sorry
0
 
LVL 16

Accepted Solution

by:
_nn_ earned 500 total points
ID: 9622141
 while (running)
  {
    DWORD word;
    // Wait for notification.
   
    dwWaitStatus = MsgWaitForMultipleObjects((DWORD)numberofhandles, ChangeHandles,
      FALSE, INFINITE,QS_POSTMESSAGE);
    if (dwWaitStatus == WAIT_OBJECT_0+numberofhandles)
    {
      MSG msg ;
     
      // Read all of the messages in this next loop,
      // removing each message as we read it.
      while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
        // If it's a quit message, we're out of here.
        if (msg.message == WM_QUIT)  
        {
          running =false; //end loop
        }
        else// Otherwise, dispatch the message.
        {
          DispatchMessage(&msg);
        }
      } // End of PeekMessage while loop.
     
    }
    else if (dwWaitStatus >= WAIT_OBJECT_0
             && dwWaitStatus < WAIT_OBJECT_0+numberofhandles)
    {
      dwWaitStatus -= WAIT_OBJECT_0;
      // now dwWaitStatus is a value between 0 and numberofhandles

      do {     //wait and see if we get multiple of the same message and skip them we only want to run once
        FindNextChangeNotification(ChangeHandles[dwWaitStatus]);
        word = WaitForSingleObject(ChangeHandles[dwWaitStatus], 1000);
       
      } while(word == WAIT_OBJECT_0);

      // I guess, the processing goes here

    }
    else
    {
      // MsgWaitForMultipleObjects may return other values
      // like WAIT_ABANDONED_0
    }

  }
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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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