Solved

multiple notifications from FindFirstChangeNotification

Posted on 2003-10-25
6
2,100 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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