🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by Danny Tue, 09 Oct 2007 05:23
To know when a file is modified
#1209
Author: Danny
Date: Tue, 09 Oct 2007 05:23
6 lines
1049 bytes
Hi all,
How can I know when a (and any) file is modified?
I don't succeed in finding the correct windows event. Currently I use a loop 
and check the time file, but isn't a good way.

Thanks in advance 
Re: To know when a file is modified
#1214
Author: Remy Lebeau (TeamB)
Date: Tue, 09 Oct 2007 09:11
12 lines
1099 bytes
"Danny" <danny2912@libero.it> wrote in message
news:470b1068@newsgroups.borland.com...

> Currently I use a loop and check the time file,
> but isn't a good way.

Look at FindFirstChangeNotification() and related functions, or at
SHChangeNotifyRegister().


Gambit
Re: To know when a file is modified
#1216
Author: Vladimir Stefanovic
Date: Tue, 09 Oct 2007 11:48
285 lines
7511 bytes
--- DirChNot.h ---
#ifndef DirChNotH
#define DirChNotH

#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>

class PACKAGE TChangeNotification : public TComponent
   {
   __published:
      __property TNotifyEvent OnChange = {read = onChange, write = 
SetOnChange};
      __property TNotifyEvent OnError = {read = onError, write = 
SetOnError};
      __property bool WatchSubFolders = {read = watchSubFolders, write = 
SetWatchSubFolders};
      __property String Folder = {read = folder, write = folder};

   public:
      __fastcall TChangeNotification(TComponent *owner);
      __fastcall ~TChangeNotification();

      void __fastcall Start();
      void __fastcall Stop();

   private:
      void __fastcall SetOnChange(TNotifyEvent event);
      void __fastcall SetOnError(TNotifyEvent event);
      void __fastcall SetWatchSubFolders(bool watch);
      void __fastcall SetFolder(String aFolder);

      TNotifyEvent onChange;
      TNotifyEvent onError;
      bool watchSubFolders;
      String folder;

      class TCNThread : public TThread
         {
         friend TChangeNotification;

         public:
            __fastcall TCNThread(TChangeNotification *owner);
            __fastcall ~TCNThread();

            void __fastcall Execute();
            void __fastcall Notify(void);
            void __fastcall Error(void);

         private:
            TChangeNotification *owner;
            HANDLE handle;
         } *thread;
   };

#endif   // ChangeNotificationH


--- DirChNot.cpp ---
#include <vcl.h>
#pragma hdrstop

#include "DirChNot.h"

DWORD dir_ch_not_flags = FILE_NOTIFY_CHANGE_FILE_NAME |
                         FILE_NOTIFY_CHANGE_LAST_WRITE;


// FILE_NOTIFY_CHANGE_FILE_NAME
// Any filename change in the watched directory or subtree causes a change
// notification wait operation to return. Changes include renaming, 
creating,
// or deleting a filename.

// FILE_NOTIFY_CHANGE_DIR_NAME
// Any directory-name change in the watched directory or subtree causes a
// change notification wait operation to return. Changes include creating or
// deleting a directory.

// FILE_NOTIFY_CHANGE_ATTRIBUTES
// Any attribute change in the watched directory or subtree causes a change
// notification wait operation to return.

// FILE_NOTIFY_CHANGE_SIZE
// Any file-size change in the watched directory or subtree causes a change
// notification wait operation to return. The operating system detects a 
change
// in file size only when the file is written to the disk. For operating
// systems that use extensive caching, detection occurs only when the cache 
is
// sufficiently flushed.

// FILE_NOTIFY_CHANGE_LAST_WRITE
// Any change to the last write-time of files in the watched directory or
// subtree causes a change notification wait operation to return. The 
operating
// system detects a change to the last write-time only when the file is 
written
// to the disk. For operating systems that use extensive caching, detection
// occurs only when the cache is sufficiently flushed.

// FILE_NOTIFY_CHANGE_SECURITY
// Any security-descriptor change in the watched directory or subtree causes 
a
// change notification wait operation to return.

__fastcall TChangeNotification::TChangeNotification(TComponent *owner) : 
TComponent(owner)
{
   onChange = 0;
   onError = 0;
   thread = 0;
   watchSubFolders = False;
}

__fastcall TChangeNotification::~TChangeNotification()
{
   delete thread;
}

void __fastcall TChangeNotification::Start()
{
   if ( thread == 0 )
      thread = new TCNThread(this);
}

void __fastcall TChangeNotification::Stop()
{
   delete thread;
   thread = 0;
}

void __fastcall TChangeNotification::SetFolder(String aFolder)
{
   if ( thread != 0 )
   {
      Stop();
      folder = aFolder;
      Start();
   }
   else
      folder = aFolder;
}

void __fastcall TChangeNotification::SetOnChange(TNotifyEvent event)
{
   if ( thread != 0 )
   {
      thread->Suspend();
      onChange = event;
      thread->Resume();
   }
   else
      onChange = event;
}

void __fastcall TChangeNotification::SetOnError(TNotifyEvent event)
{
   if ( thread != 0 )
   {
      thread->Suspend();
      onError = event;
      thread->Resume();
   }
   else
      onError = event;
}

void __fastcall TChangeNotification::SetWatchSubFolders(bool watch)
{
   if ( thread != 0 )
   {
      Stop();
      watchSubFolders = watch;
      Start();
   }
   else
      watchSubFolders = watch;
}

__fastcall TChangeNotification::TCNThread::TCNThread(TChangeNotification 
*owner) : TThread(false), owner(owner)
{
   handle = 0;
}

__fastcall TChangeNotification::TCNThread::~TCNThread()
{
   Terminate();

   if ( handle != 0 )
   {
      FindCloseChangeNotification( handle );
      handle = 0;
   }
}

void __fastcall TChangeNotification::TCNThread::Execute()
{
   handle = FindFirstChangeNotification( owner->Folder.c_str(), 
owner->WatchSubFolders, dir_ch_not_flags );

   if ( handle == INVALID_HANDLE_VALUE )
   {
      Synchronize( Error );
      Terminate();
   }

   while ( !Terminated )
   {
      DWORD status = WaitForSingleObject( handle, INFINITE );

      if ( status == WAIT_OBJECT_0 && Terminated == false )
      {
         Synchronize( Notify );

         if ( FindNextChangeNotification( handle ) == 0 )
         {
            Synchronize( Error );
            Terminate();
         }
      }
      else
      {
         Terminate();
      }
   }
}

void __fastcall TChangeNotification::TCNThread::Notify(void)
{
   if ( owner->OnChange ) owner->OnChange( owner );
}

void __fastcall TChangeNotification::TCNThread::Error(void)
{
   if ( owner->OnError ) owner->OnError( owner );
}

static inline void ValidCtrCheck(TChangeNotification *)
{
   new TChangeNotification( NULL );
}

#pragma package(smart_init)

--- MAIN.CPP ---
// ...
#include "DirChNot.h"
// ...
TChangeNotification *note; // or in class header
// ...
CONSTRUCTOR()
{
   // ...
   note = new TChangeNotification ( this );
   note->Folder =  CONFIG_PATH;
   note->WatchSubFolders = false;
   note->OnChange = YourCode;
   // ...
}
DESTRUCTOR()
{
   // ...
   note->Stop();
   delete note;
   // ...
}
void __fastcall TYourForm::YourCode(TObject* Sender)
{
   // Your Code Here ...
}

--- MAIN.H ---
// ...
   private:
   void __fastcall YourCode(TObject* Sender);
// ...



-- 
Best Regards,
Vladimir Stefanovic
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads