Solved

File changed notification

Posted on 2000-05-07
3
158 Views
Last Modified: 2013-11-20
Hi,
I need to do the following in my MDI application:
If the file associated with the active view changes by other application then when the user returns to my application
one message box must warn this.

I have think to do the following: Save the modified time and each the OnSetFocus message is sent to the view, compare it with a new modified time reading.
The problem is that this message is always being sent after the message box ends. More each window in my application that loses focus to my view will trigger this message.
Can I filter this to only accept OnSetFocus from alien windows?
Is there a better way to spy file changes?

I am looking for comments on my solution and of course how to solve the problem if this is the right way to do things.
0
Comment
Question by:lsimaozinho
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 2786003
The easiest way to do this is opening a notification handle for this file in a separate thread and send a message to your application when the file is modified, e.g.

('pseudocode', error checking omitted for brevity)

HANDLE      g_hFileWatcher;
DWORD       g_dwTID;

    g_hFileWatcher  =   CreateThread    (   NULL,
                                            0,
                                            FileWatcher,
                                            (PVOID) "c:\\docs\\whatever.txt",
                                            0,
                                            &g_dwTID
                                        );


int WINAPI FileWatcher  (   LPVOID  pv)
{
    HANDLE  hNotify;
    DWORD   dwWaitResult;
    char*   pszFile =   ( char*) pv;

    OutputDebugString   (   "FileWatcher(): here i am!!!\n");

    hNotify =   FindFirstChangeNotification (   pszFile,
                                                FALSE,
                                                FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE
                                            );

    if  (   INVALID_HANDLE_VALUE    ==  hNotify)
            OutputDebugString   (   "Error acquiring handle file!!!\n");

    for (   ;;)
        {
            dwWaitResult    =   WaitForSingleObject (   hNotify,
                                                        INFINITE
                                                    );

            if  (   WAIT_ABANDONED  ==  dwWaitResult)
                    return( dwWaitResult);

            OutputDebugString   (   "FileWatcher(): there has been some change...\n");

            SendMessage ( WM_THEFILEHASCHANGED, ...)

            FindNextChangeNotification  (   hNotify);
        };

    return( 0);
}

Feel free to ask if you need more information!
0
 
LVL 86

Expert Comment

by:jkr
ID: 2786097
Ooops, forgot something - be sure to insert 'CloseHandle ( hNotify);' before the 'return' statement... Sorry ;-)
0
 

Author Comment

by:lsimaozinho
ID: 2786109
Thanks jkr
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
substring method in java 1 79
VBA "SendKeys" Syntax for Multiple Keystrokes 7 98
Problem to event 3 52
Thin secure Windows 10 5 49
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now