The Code Project View our sponsorsGet your new domain name now!Advertise on the CodeProject
Home >> Files & Folders >> General

CFileChangeEvent Class
By Franky Braem

An article about how we can notify our application that a file has been changed by another application. 
 VC++, Win9x, NT4
 Posted 1 Dec 1999
Articles by this author
Send to a friend
Printer friendly version
Lounge New Articles Sign in Forums Contribute
Broken links? Email us!
3 users have rated this article. result:
5 out of 5.

  • Download demo project - 37 Kb
  • Download source files - 4 Kb
  • This article is about how we can notify our application that a file has been changed by another application.

    I've created the CFileChangeEvent-class for it. It has the following methodes :

    void addFile(const std::string &sFileName)
    Use this method when you want to be notified of changes of a file. When the notification thread is running it will be stopped and restarted.
    void removeFile(const std::string &sFileName)
    Use this method when you don't want to be notified anymore. When the notification thread is running it will be stopped and restarted.
    void startWatch(void)
    This method starts the notification thread.
    void stopWatch(void)
    This method stops the notification thread. It's also called in the destructor, so you the thread will allways stop.
    virtual void OnFileAlarm(FileAlarm nAlarm, const std::string &sFileName)
    Override this method. This method is called when a file is changed or deleted. FileAlarm is an enum with following attributes : FA_CHANGED, FA_REMOVED, FA_CREATED.

    How to use this class ?

    Derive a class (for example your CDocument-class) from CFileChangeEvent and override the OnFileAlarm. That's all.

    Warning You can't call the method UpdateAllViews in the OnFileAlarm. This is why :

    A thread can access only MFC objects that it created. This is because temporary and permanent windows handle maps are kept in thread local storage to ensure protection from simultaneous access from multiple threads.

    How can we solve this ?

    1. pass the individual handles(like HWND) rather than C++ objects to the worker thread. The thread then adds these objects to its temporary map by calling the appropiate FromHandle member function.
    2. You can use a new user-defined message corresponding to the different tasks your worker threads will be performing and post these messages to the application's windows/views using PostMessage.

      2.1 Create a user-defined message with ::RegisterWindowMessage like this :

            const UINT msgFileChange = ::RegisterWindowMessage("FILECHANGE");

      put this in the Application's header file so that you can use this everywhere in your application

      2.2 In your document-class create a member-variable to hold a pointer to the window you want to process the message.

            CView *m_pView; // This variable must be initialized in your view-class.

      2.3 In the method called by the thread post the message to the processing window.

            m_pView->PostMessage(msgFileChange); // You can pass wparam, lparam if you wish

      2.4 In the processing view headerfile add the following in the message map functions

            afx_msg void OnFileChange(); // when you pass wparam, lparam add parameters to this function

      2.5 In the processing view sourcefile add the following in the Message Map

            ON_REGISTERED_MESSAGE(msgFileChange, OnFileChange)

      2.6 Add the sourcecode for OnFileChange in the processing view sourcefile

          void CProcessView::OnFileChange()
          {
              CMyDocument *pDoc = GetDocument();
              pDoc->UpdateAllViews();
          }
      

    About Franky Braem

    Currently working at KBC Insurance in Belgium.
    Programmer since 1991 using C/C++/Visual Basic and Java. Playing with ATL/WTL at home.

    Click here to visit Franky Braem's homepage.

    [Top] Sign in to vote for this article:     PoorExcellent  
    Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
     Keyword Filter
     View   Per page   Messages since
    New threadMessages 1 to 4 of 4 (Total: 4)First Prev Next Last
    Subject 
    Author 
    Date 
      Incorrect notifications fired.
    Rob Wainwright 8:59 19 Oct 00 
      Change notifcations between 95/98 and NT
    Dean Wyant 16:36 2 Apr 00 
      Re: Change notifcations between 95/98 and NT
    Selva 0:18 7 Apr 01 
      Missing Project File
    nn 12:56 7 Feb 00 
    Last Visit: 12:00 Friday 1st January, 1999First Prev Next Last

    Home >> Files & Folders >> General
    last updated 1 Dec 1999
    Article content copyright Franky Braem, 1999
    everything else © CodeProject, 1999-2001.
    The Code Project View our sponsorsClick here for Dundas Software - 3D accelerated presentation quality ASP charting Advertise on the CodeProject