Reputation: 32797
I want to collect the path of the files as soon as any file is created in a particular folder.
I used a List<string>
and the FileSystemWatcher
component. I add the paths to the List
in the Created
event, and everything works fine.
However, when there are many small files created, say around 2000, the Created
event is fired only 1200 times. When I don't add the path to the list in the Created
event, though, it is called for 2000 times. I tried using a separate thread, but to no avail.
How can I enable FileSystemWatcher
to fire each and every event without missing some of the events?
Upvotes: 1
Views: 907
Reputation: 13947
I have found, IIRC, that the FileSystemWatcher
will not 'see' any new files while it is processing a given Created
event. That is, while the Created
event handler is processing, any new files created do not raise the event.
Have you tried making the processing faster (kick off an async thread, perhaps)? I know you mentioned a separate thread, but without code, it's hard to tell what that thread is doing or how it is used.
Upvotes: 1
Reputation: 7591
chances are it's a race condition and 2 files are added to the list at the same time. last write wins. locking the list before adding to the list would prevent this. If you are using .net 4.0 you could also use a concurrent dictionary or readwriteslim to assist with the concurrency issues.
Upvotes: 4
Reputation: 941495
Be sure to implement the Error event so you know when things go wrong. Keep the notification event handlers short and snappy, just add the file to a list and get out. Process the list in another worker thread. Increasing InternalBufferSize can help but should be avoided.
Upvotes: 3