Giter Club home page Giter Club logo

rdcfswatcherexample's People

Contributors

kovbal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rdcfswatcherexample's Issues

Watcher Does not close properly

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.