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,
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
ReadDirectoryChangesWFunction. I also liked the Post onMedium.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:I just added the Sleep to simulate some long running work based on the File Event.
If you now for example add
10 Filesto 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
WatchInfosThis is the version of the Function:
I don't know if this leads to some other problems but with this change the termination of the Program works as expected.