tresorit / rdcfswatcherexample Goto Github PK
View Code? Open in Web Editor NEWThis is a working example for using ReadDirectoryChangesW() with IOCPs.
License: BSD 3-Clause "New" or "Revised" License
This is a working example for using ReadDirectoryChangesW() with IOCPs.
License: BSD 3-Clause "New" or "Revised" License
Hello,
first of all i want to thank you for the code you provide here this helps me alot because i was looking for some nice example on how to use the ReadDirectoryChangesW
Function. I also liked the Post on Medium
.
I think i found a little bug in your implementation which could lead to an endless run of the Watcher.
For this i attached the following Function to the changeEvent
:
watch.changeEvent = [](int64_t id, const std::set<std::pair<std::wstring, uint32_t>>& notifications) {
for (const auto& notif : notifications) {
std::wcout << L"Change on watcher with ID=" << id <<
L", relative path: \"" << notif.first.c_str() << L"\"" << L"event: " <<
std::to_wstring(notif.second).c_str() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
};
I just added the Sleep to simulate some long running work based on the File Event.
If you now for example add 10 Files
to the Directory and Hit a Key inside the Console Window during the processing of the 10 Files the application tries to close the Eventloop. If you now adding more Files for example to the Directory these events will also processed (which should not happen because the application should close).
You can now make changes to the directory as long as some events are still processed and the Loop will not be closed successfully.
To close the Application successfully i made a change to the FsWatcher::stopEventLoop
.
I just switched the Statements and first put the completition Message on the Queue and then try to remove the WatchInfos
This is the version of the Function:
void FsWatcher::stopEventLoop() {
if (this->iocp.get() != INVALID_HANDLE_VALUE) {
std::cout << "Post Completion Status\n";
auto res = PostQueuedCompletionStatus(this->iocp.get(), 0, reinterpret_cast<ULONG_PTR>(this), nullptr);
if (res) {
std::cout << "Successfully posted completition status\n";
} else {
std::cout << "Could not post completition status " << GetLastError() << " \n";
}
}
{
std::cout << "Stop Event Loop tries to get Lock\n";
std::lock_guard<std::mutex> lock(this->watchInfoMutex);
std::cout << "Stop Event Loop gets the lock\n";
for (auto& watchInfo : this->watchInfos) {
watchInfo.second.stop();
}
}
}
I don't know if this leads to some other problems but with this change the termination of the Program works as expected.
Hello,
When I create a new file or directory in Explorer, the watcher program only prints this line:
Change on watcher with ID=1, relative path: "
Then the whole program cannot monitor any more.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.