🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10039

Re: Using Damon Chandler's FindNextChangeNotification example

#10039
From: JD
Date: Sun, 23 May 2004 16:49
153 lines
6810 bytes
"Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> wrote:
> [...] However, you have not shown all of your own code, so I
> cannot compare the two. Since you mentioned that you did
> make some changes, I would have to suspect that you probably
> changed something that you should't have.

I still get double notification with a single change using
WaitForMultipleObjects and no notification at all if I use
WaitForSingleObject (which does not error).

__fastcall TForm1::TForm1(TComponent* Owner) : TForm( Owner )
{
    MonitorThread = new TMonitorThread( true, this );
    MonitorThread->SetWatchPath( "C:/Test/TestMonitor" );
    MonitorThread->Resume();
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
    // move from OnClose event to d'tor
    MonitorThread->Terminate();
}
//-------------------------------------------------------------
// Overrode WndProc method instead of using a message map
// Tested using the map and same results as I expected it should
void __fastcall TForm1::WndProc( TMessage &Message )
{
    if( Message.Msg == APP_FILECHANGE_NOTIFICATION )
      {
          String s;
          if( Message.LParam == WAIT_ABANDONED )
            {
                s.sprintf("WaitForSingleObject failed with error code : %d", Message.WParam );
            }
          else
            {
                s.sprintf("A file in the folder changed.\nThe notification counter reads : %d.", Message.WParam );
            }
          ShowMessage( s );
      }
    else TForm::WndProc(Message);
}
//-------------------------------------------------------------

#ifndef MonitorThreadH
#define MonitorThreadH
//-------------------------------------------------------------
#include <Classes.hpp>
//-------------------------------------------------------------
// this was defined as : #define CM_DIRCONTENTSCHANGED WM_APP + 101
#define APP_FILECHANGE_NOTIFICATION ( WM_USER + 3 )

class TMonitorThread : public TThread
{
    private:
        // this was defined as a TControl*
        TForm* FTargetForm;
        HANDLE FChangeHandle;
        AnsiString FWatchPath;
        // made global for testing
        DWORD dwStatus;
        // added for testing
        DWORD Counter;
    protected:
        void __fastcall Execute();
    public:
        void __fastcall MonitorTerminate(TObject *Sender);
        // renamed function
        void __fastcall NotifyApplication();
        // renamed parameter from 'Value' to 'Path'
        void __fastcall SetWatchPath( AnsiString Path );
        // change the c'tor to cast 'this' to a TForm* instead of TControl*
             __fastcall TMonitorThread(bool CreateSuspended, TForm* TargetForm );
};
//-------------------------------------------------------------
#endif

//-------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "MonitorThread.h"
//-------------------------------------------------------------
__fastcall TMonitorThread::TMonitorThread(bool CreateSuspended, TForm* TargetForm ) : TThread(CreateSuspended)
{
    FreeOnTerminate = true;
    FChangeHandle = INVALID_HANDLE_VALUE;
    FTargetForm = TargetForm;
    FWatchPath = "";
    OnTerminate = MonitorTerminate;
}
//-------------------------------------------------------------
void __fastcall TMonitorThread::MonitorTerminate(TObject *Sender)
{
    if( FChangeHandle != INVALID_HANDLE_VALUE ) FindCloseChangeNotification( FChangeHandle );
}
//-------------------------------------------------------------
void __fastcall TMonitorThread::SetWatchPath( AnsiString Path )
{
    if( FWatchPath != Path )
      {
          FWatchPath = Path;
          if( FChangeHandle != INVALID_HANDLE_VALUE ) FindCloseChangeNotification( FChangeHandle );
          // minor deletion of code because I only want one flag
          FChangeHandle = INVALID_HANDLE_VALUE;
          FChangeHandle = FindFirstChangeNotification( Path.c_str(), false, FILE_NOTIFY_CHANGE_LAST_WRITE );
      }
}
//-------------------------------------------------------------
void __fastcall TMonitorThread::Execute()
{
    Counter = 0;
    while( !Terminated && FChangeHandle != INVALID_HANDLE_VALUE )
      {
          bool TimedOut = true;
          while( TimedOut && !Terminated && !Suspended )
            {
//                dwStatus = WaitForMultipleObjects(1, &FChangeHandle, false, 2000 );
                dwStatus = WaitForSingleObject( &FChangeHandle, 2000 );
                if( dwStatus == WAIT_OBJECT_0 && !Terminated )
                  {
                      ++Counter;
                      Synchronize( NotifyApplication );
                      bool reset = FindNextChangeNotification( FChangeHandle );
                      if( !reset ) break;
                  }
                else if( dwStatus == WAIT_TIMEOUT && !Terminated )
                  {
                      TimedOut = true;
                  }
                // added to check failure of WaitForSingleObject
                else if( dwStatus == WAIT_ABANDONED )
                  {
                      Counter = GetLastError();
                      Synchronize( NotifyApplication );
                      TimedOut = false;
                  }
                else TimedOut = false;
            }
      }
    Terminate();
}
//-------------------------------------------------------------
void __fastcall TMonitorThread::NotifyApplication()
{
    if( FTargetForm )
      {
          // this was a call to a TControl::Perform
          PostMessage( FTargetForm->Handle, APP_FILECHANGE_NOTIFICATION, Counter, dwStatus );
      }
}
//-------------------------------------------------------------
Technical Details
Message-ID: <40b0d61d$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40ade94f$1@newsgroups.borland.com> <40af63b7$1@newsgroups.borland.com> <40af8def$1@newsgroups.borland.com> <40afda3f$1@newsgroups.borland.com> <40b0233e$1@newsgroups.borland.com> <40b030d4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,32a59cffbb6bea4a
X-Google-Attributes: gid10cc67,public
From: "JD" <nospam@nospam.com>
Sender: "JD" <nospam@nospam.com>
Reply-To: "JD" <nospam@nospam.com>
Subject: Re: Using Damon Chandler's FindNextChangeNotification example
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 172.138.120.157     
References: <40ade94f$1@newsgroups.borland.com> <40af63b7$1@newsgroups.borland.com> <40af8def$1@newsgroups.borland.com> <40afda3f$1@newsgroups.borland.com> <40b0233e$1@newsgroups.borland.com> <40b030d4$1@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <40b0d61d$1@newsgroups.borland.com>
Date: 23 May 2004 09:49:33 -0700
X-Trace: newsgroups.borland.com 1085330973 207.105.83.66 (23 May 2004 09:49:33 -0700)
Lines: 155
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail