|
Broken links? Email us! |
|
This article is about how we can notify our application that a file has been changed by another application.
I've created the CFileChangeEvent-class for it. It has the following methodes :
void addFile(const std::string &sFileName)
Use this method when you want to be notified of changes of a file. When the notification thread is running it will be stopped and restarted.
void removeFile(const std::string &sFileName)
Use this method when you don't want to be notified anymore. When the notification thread is running it will be stopped and restarted.
void startWatch(void)
This method starts the notification thread.
void stopWatch(void)
This method stops the notification thread. It's also called in the destructor, so you the thread will allways stop.
virtual void OnFileAlarm(FileAlarm nAlarm, const std::string &sFileName)
Override this method. This method is called when a file is changed or deleted. FileAlarm is an enum with following attributes : FA_CHANGED, FA_REMOVED, FA_CREATED.
Derive a class (for example your CDocument-class) from CFileChangeEvent and override the OnFileAlarm. That's all.
Warning You can't call the method UpdateAllViews in the OnFileAlarm. 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.
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();
}
Currently working at KBC Insurance in Belgium.
Programmer since 1991 using C/C++/Visual Basic and Java. Playing with ATL/WTL at home.
Click here to visit Franky Braem's homepage.
|
|