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

  • Live Event Date: June 30, 2016 @ 1:00 p.m. ET / 10:00 a.m. PT There's a disconnect happening in our mobile strategies. We have top-down mandate that our organizations should be "mobile first" and undergo "digital transformations" without a lot of additional insight as to how that should become a reality. In the meantime, members within our organizations inherently understand the benefits of mobile apps -- and, understandably, they want to benefit from them. But the resources to bring an app into existence …

  • Enterprises are typically overwhelmed with incredible volumes of alerts that lack context and are not easily correlated across their technology stacks and silos. It can be incredibly challenging for large enterprises to detect and resolve incidents across multiple environments. When enterprises do detect challenges, communicating and acting efficiently through the right tools can be a daunting proposition. IT teams can provide real value if they collaborate and use agile methods for enterprise DevOps to move …

Most Popular Programming Stories

More for Developers

RSS Feeds

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