Question regarding filesystemwatcher and existing files
-
I'm working on an app that utilizes a filesystemwatcher to detect for a particular type of file. When the watcher is running, everything is fine. The watcher correctly picks up new files. However, the app needs to support being paused and restarted. So, if the app is paused, files are then copied into the directory and the watcher is restarted - it doesn't pick up the existing files even though they match the filter. I realize the watcher wouldn't trigger the created or modified events, but is there a way to make the watcher to pick up these files, or do I just need to code around this? Just wanted to make sure I wasn't overlooking something with the filewatcher itself.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
I'm working on an app that utilizes a filesystemwatcher to detect for a particular type of file. When the watcher is running, everything is fine. The watcher correctly picks up new files. However, the app needs to support being paused and restarted. So, if the app is paused, files are then copied into the directory and the watcher is restarted - it doesn't pick up the existing files even though they match the filter. I realize the watcher wouldn't trigger the created or modified events, but is there a way to make the watcher to pick up these files, or do I just need to code around this? Just wanted to make sure I wasn't overlooking something with the filewatcher itself.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
The watcher will only do things it is designed for. It will not detect files that have been added while the watcher is paused. Why pause the App? why not leave it running and just handle the events while the file copy operation is in progress. system.io.filesystemwatcher[^]
-
The watcher will only do things it is designed for. It will not detect files that have been added while the watcher is paused. Why pause the App? why not leave it running and just handle the events while the file copy operation is in progress. system.io.filesystemwatcher[^]
I don't come up with the design requirements, I'm just tasked with "getting it done". Just checking to make sure that the filewatcher doesn't support this action. Thanks though.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
I don't come up with the design requirements, I'm just tasked with "getting it done". Just checking to make sure that the filewatcher doesn't support this action. Thanks though.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Well, here's an alternative solution... Instead of pausing the FSW, pause the handler. When you get an event, check to see whether your handler is "paused"... If so, stick the event in a Queue instead. When unpausing, process the Queue first.
Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)
-
Well, here's an alternative solution... Instead of pausing the FSW, pause the handler. When you get an event, check to see whether your handler is "paused"... If so, stick the event in a Queue instead. When unpausing, process the Queue first.
Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)
-
Admittedly, I've never heard of pausing a handler. Could you point me in the right direction?
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Something like this (pseudocode):
Class Watcher
Private System.IO.FileSystemWatcher
Private Bool IsPaused
Private QueuePublic Sub Pause()
IsPaused = true
End SubPublic Sub UnPause()
While Queue is not empty
ProcessFSWEvent(next item in queue)
IsPaused = false
End SubPrivate Sub FileSystemWatcherEventHandler(sender, eventargs)
If IsPaused Then
Add eventargs to Queue
Else
ProcessFSWEvent(eventargs)
End SubPrivate Sub ProcessFSWEvent(eventargs)
Do whatever you'd normally do with the event
End SubProud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)