How do I clear unfinished FileSystemWatcher events?

I need to make sure all pending FileSystemWatcher events are handled prior to my operation. Is there a way to do this?

+2


source to share


4 answers


My solution for this task. So far it looks okay.



while (true)
{
    WaitForChangedResult res = watcher.WaitForChanged(WatcherChangeTypes.All, 1);
    if (res.TimedOut)
         break;
}

      

+1


source


I know this post is old, but I just did it ... You were almost there skevar7.

You just need to write a temp file to the folder you are looking at and wait to receive the Created event. In the generated case, you will set a flag indicating that the wait loop should exit. Your temp file will be the last one in the notification queue, so you know it's flushed.



If you have other streams constantly writing to the watched folder, you will need to block them while this is done to ensure it turns completely red.

+1


source


FileSystemWatcher uses an unloaded memory buffer to store file system events. When you process each event, it is removed from this buffer, making room for new events.

It is not a queue or a list that can grow indefinitely. If many events occur in a short time, the buffer is filled and then an error event is raised. Thus, you have no way of flushing that buffer.

If you want the buffer to be empty before starting to handle events, the simplest approach would be to create a new FileSystemWatcher and start watching just before using it. However, you must be careful that you do not create too many concurrent observers due to the fact that each of them stores a buffer in unloaded memory.

0


source


If you are looking at the file, open it for writing to lock it so that no one else can access it and thus generate any new events. Then wait a millisecond or so. Nasty? Yes.

0


source







All Articles