watching for file modifications? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

watching for file modifications?

Post by MoonKid » Sat Feb 12, 2011 11:27 am

I have a software A that run in background all the time. When it start it read something like a config-file.

I have a software B that sometimes change this config-file.

Now software A need to know (while running) that its config-file was modified to read it again.

Understand?
How can I do this?

Of course A could have timer that look for the file-timestamp every 10 seconds. But I don't want it that way.

Is there something like a operating-system (Win7) event that is fired when a file is modified? Can wx handle this?

Or is there a wx-way to communicate between A and B. So B can say to A: "Hey I modified your config-file. Read it again!" ?

User avatar
doublemax
Moderator
Moderator
Posts: 14617
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Sat Feb 12, 2011 12:20 pm

Is there something like a operating-system (Win7) event that is fired when a file is modified? Can wx handle this?
In wx 2.9.x there is wxFileSystemWatcher
http://docs.wxwidgets.org/trunk/classwx ... tcher.html
Or is there a wx-way to communicate between A and B. So B can say to A: "Hey I modified your config-file. Read it again!" ?
http://docs.wxwidgets.org/stable/wx_ipc ... pcoverview
It's actually easier to use than it looks at first sight ;)
Check the "ipc" sample.
Use the source, Luke!

MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

Post by MoonKid » Sun Feb 13, 2011 7:26 pm

doublemax wrote:In wx 2.9.x there is wxFileSystemWatcher
http://docs.wxwidgets.org/trunk/classwx ... tcher.html
I used to do it like in the example in the doc. It build and run. But changes of the file are ignored.

The doc says I should override

Code: Select all

wxFileSystemWatcher::OnChange
I did but nothing happen. And I can not find any methode like it in the wx-code.

I think the doc are not consistent with the current state of wxFileSystemWatcher?

Code: Select all

class WXCrontabWatcher : public wxFileSystemWatcher
{
    protected:
        ///
        void OnChange(int changeType, const wxFileName& path, const wxFileName& newPath);

    public:
        /// ctor
        WXCrontabWatcher ();

        /// virtual dtor
        virtual ~WXCrontabWatcher ();
};

void WXCrontabWatcher::OnChange(int changeType, const wxFileName& path, const wxFileName& newPath)
{
    wxMessageBox("on change", path.GetFullPath());
}

void WXCApp::OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
{
    pCrontabWatcher_ = new WXCrontabWatcher();

    pCrontabWatcher_->Add( wxFileName::DirName(GetCrontabFilename()), wxFSW_EVENT_MODIFY);
}

User avatar
doublemax
Moderator
Moderator
Posts: 14617
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Sun Feb 13, 2011 8:20 pm

The documentation is wrong there, overriding the virtual method does not work, you have to catch the events.

Check this thread:
http://forums.wxwidgets.org/viewtopic.php?t=29798
Use the source, Luke!

Post Reply