WebFreak001 / FSWatch

A cross-platform folder & file watching library using win32, inotify or std.file
33 stars 8 forks source link

Multiple dynamic watchers #13

Closed bausshf closed 6 years ago

bausshf commented 6 years ago

Is it possible to create multiple watchers and store them in an array and then loop that array in a while loop to watch for events?

I can't seem to get that working and I suspect it doesn't work, because FileWatch is a struct?

Perhaps if FileWatch was a class it would work?

Basically what I'm doing now is this: (Minimal example)

I create an array like:

FileWatch[] watchers;

Then I load the watchers from a config file with the paths:

foreach (path; paths)
{
    watchers ~= FileWatch(path, true);
}

And then I do this:

while (true)
{
    foreach (watcher; watchers)
     {
        foreach (event; watcher.getEvents())
        {
            // It never gets anything here ...
        }
    }
}

Maybe I'm using it wrong?

Also I'm on Windows. Could it be a Windows issue?

WebFreak001 commented 6 years ago

oh yeah that will be the case, you could just make them pointers (FileWatch*[] watchers; watchers ~= new FileWatch(path, true);) though.

OR if you just foreach over it like you do foreach by ref: foreach (ref watcher; watchers)

bausshf commented 6 years ago

Yeah, using pointers work, but meh.

I'll close this ig.

SrMordred commented 5 years ago

This is not working.

auto sw  = FileWatch("shader", true);
FileWatch[] watchers;
watchers~= sw;
//loop

//don´t show anything
foreach (event; watchers[0].getEvents())
    event.print;

//show properly
foreach (event; sw.getEvents())
    event.print;

looking at the code now, but not seeing anything wrong, yet.

SrMordred commented 5 years ago

Ok got it. In my case (Windows), once you create the FileWatch, you already queue that into windows ReadDirectoryChangesW but the change buffer is a new one on the copy stored on the array. So if you don´t call getEvents() on FileWatch ctor or you set the queue to false again(watchers[0].queue = false;), it works properly. (but queue is private btw :P)