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



Comments

  • 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,
    FALSE,FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_FILE_NAME);

    The return value is always 0xFFFFFFFF.

    Will ask this question at experts-exchange.com.

    Bye,
    Michael Kuelshammer

    Reply
  • 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)

    //snip
    HANDLE hFileChanged=FindFirstChangeNotification (path,FALSE,FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_FILE_NAME);

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

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

    FindCloseChangeNotification(hFileChanged);

    if(dwResult==WAIT_OBJECT_0+1)
    //endsnip

    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.

    Thanks,

    Joe

    Reply
  • 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();
    pDoc->UpdateAllViews();
    }

    Reply
  • 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().

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

Top White Papers and Webcasts

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds