Click here to Skip to main content
Licence 
First Posted 30 Nov 1999
Views 79,606
Bookmarked 47 times

CFileChangeEvent Class

By | 30 Nov 1999 | Article
An article about how we can notify our application that a file has been changed by another application.

Introduction

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 methods:

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 know the thread will always 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 appropriate 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.
    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. 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.
    3. In the method called by the thread, post the message to the processing window.
      // You can pass wparam, lparam if you wish
      m_pView->PostMessage(msgFileChange);
    4. In the processing view header file, add the following in the message map functions:
      // when you pass wparam, lparam add parameters to this function
      afx_msg void OnFileChange();
    5. In the processing view source file, add the following in the message map:
      ON_REGISTERED_MESSAGE(msgFileChange, OnFileChange)
    6. Add the source code for OnFileChange in the processing view source file.
      void CProcessView::OnFileChange()
      {
          CMyDocument *pDoc = GetDocument();
          pDoc->UpdateAllViews();
      }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Franky Braem

Web Developer

Belgium Belgium

Member

Programmer since 1991 using C/C++/Visual Basic and Java. Playing with wxWindows at home.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalwhat about Copy Event? Pinmemberbatsword19:21 5 May '11  
Generalusage in CDialog Pinmemberpicazo9:45 5 Oct '05  
GeneralImportant Fix in CFileChangeEvent::addFile PinmemberDavid Lantsman10:15 24 Mar '05  
GeneralRegsitry Change Notification PinmemberRoyce Fickling14:14 14 Aug '04  
GeneralFile Removed Event PinmemberPaul-T23:12 13 Dec '03  
GeneralRe: File Removed Event PinmemberAlvaro Palma10:21 30 Jul '04  
GeneralPrevent a file from being deleted Pinmemberja1618822:32 17 Dec '02  
GeneralProblem with removeFile PinmemberVinicius Pontes4:18 31 Jul '02  
Generalcan't call the method UpdateAllViews - Simple Solution PinmemberAmit Gefen15:11 29 Nov '01  
QuestionWatching remote files (not mapped)??? PinmemberDaniel Madden6:59 10 Nov '01  
GeneralIncorrect notifications fired. PinsussRob Wainwright2:59 19 Oct '00  
GeneralChange notifcations between 95/98 and NT PinsussDean Wyant10:36 2 Apr '00  
GeneralRe: Change notifcations between 95/98 and NT PinmemberSelva18:18 6 Apr '01  
GeneralMissing Project File Pinsussnn6:56 7 Feb '00  
GeneralRe: Missing Project File Pinmemberdarius3:08 8 Dec '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 1 Dec 1999
Article Copyright 1999 by Franky Braem
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid