Alerting the user when an open file has been changed by another application

Like Visual Studio this demo-application alarms the user if any changes made in a file, wich is open in a document in this time.

Everytime, when a document was opened, or saved as.. the document-class starts a thread. In this thread, changes will detected in the current directory with a FinfFirstChangeNotification()-Event. If this event is signaled by the system the last-write-time of the actual file will be compared. If the comparison failed, the alarm will start.

If the main window isn't at the top, the icon in the taskbar will flash until the user activate the app.

To test the demo, open a textfile, open the same textfile with notepad. When you save the notepad text, then the alarming will become apparent.

Another test is renaming an open textfile with the windows explorer.

Download demo project - 47 KB

Date Last Updated: April 24, 1999


  • Can't get it work under Windows 95

    Posted by Legacy on 05/13/1999 12:00am

    Originally posted by: Michael Kuelshammer

    Tried also another example from Dundas Software,
    COXDirWatch-class, it also doesn't work under windows 95.
    I never get a handle from
    HANDLE hFileChanged=FindFirstChangeNotification(path,

    The return value is always 0xFFFFFFFF.

    Will ask this question at

    Michael Kuelshammer

  • Can't get it working under 95

    Posted by Legacy on 05/10/1999 12:00am

    Originally posted by: Joe Finnerty

    Under Windows 95 I couldn't get it to work. I did get it
    working under NT. It appears to be failing in this
    section: (I am not using a network directory)


    HANDLE hWaitEvents[]={pDoc->m_evStopWatch,hFileChanged};

    DWORD dwResult=WaitForMultipleObjects(2,hWaitEvents,FALSE,INFINITE);



    I don't think the WaitForMultipleObjects is waiting
    because the (dwResult ==...) never returns true
    and it breaks out of the while loop

    Any ideas greatly appreciated.



  • Solution for UpdateAllViews

    Posted by Legacy on 05/06/1999 12:00am

    Originally posted by: Franky Braem

    UpdateAllViews does not work when called in the OnFileAlarm-method.
    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();

  • Using FindNextChangeNotification

    Posted by Legacy on 04/26/1999 12:00am

    Originally posted by: Paul Reynolds

    I noticed that the code closes the handle as soon as a file is found...

    FindNextChangeNotification() is provided so that you don't need to keep closing and calling FindFirstChangeNotification().

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Data integrity and ultra-high performance dictate the success and growth of many companies. One of these companies is BridgePay Network Solutions, a recently launched and rapidly growing financial services organization that allows merchants around the world to process millions of daily credit card transactions. Due to the nature of their business, their IT team needed to strike the perfect balance between meeting regulatory-mandated data security measures with the lowest possible levels of latency and …

  • As virtualization becomes the norm throughout organizations of nearly all sizes, and as more organizations look to private cloud solutions, IT decision makers are increasingly in need of ways to keep storage costs and complexity under control in the face of often-runaway virtual machine (VM) sprawl. Application-aware storage is designed to help achieve these important goals. Read this white paper to learn how application-aware storage allows you to gain VM-level visibility into application performance and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date