Re: FileSystemWatcher advice required please
- From: "Kevin Spencer" <kevin@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 14 Feb 2006 08:23:20 -0500
Hi Jimmy,
The Created event occurs once per each file created. In addition, I see that
you're attaching the same event handler to the Changed event. This was not
what I suggested. To quote my earlier replies:
First reply:
"When a file is created, the FileSystemEventArgs passed with the event
contains the path of the file created. You can use this
information to move the file to the new location.No need to create a list of
files to move. **Process each one at a time.**"
Second reply:
"Why are you using OnChange? If you remember my first reply, I told you that
I assumed you were using the OnCreated event. This is because you said that
the files were being "dropped" into the directory. This causes a Create
event to occur, and that should be the only event that you need to listen
for."
You know, my mother used to tell me about people who loved her recipes for
one thing or another. But they would come back and tell her that when they
used her recipe, it just didn't taste as good as my mother's. Invariably,
she found out that they had "improvised" a bit on her recipe, and that this
was the reason it didn't taste the same!
I'm thinking that perhaps you are misunderstanding something about these
files being dropped into the folder, because of a question you asked earlier
in this thread:
"Am I correct to assume that only 1 event is triggered regardless of the
number of files dropped into the directory? It would
be a lot easier/better if a separate event was triggered for each file."
At the time you were asking about the Change event, but I believe you
misunderstand the idea of "dropping multiple files" in general. When you
drop multiple files into a folder, they are created one at a time. It
happens so fast that it may look like it's doing more than one at a time,
but it's not.
Also, multiple events are fired during different types of IO operations.
Take a note of this Note from the SDK:
"Common file system operations might raise more than one event. For example,
when a file is moved from one directory to another, several OnChanged and
some OnCreated and OnDeleted events might be raised. Moving a file is a
complex operation that consists of multiple simple operations, therefore
raising multiple events. Likewise, some applications (for example,
anti-virus software) might cause additional file system events that are
detected by FileSystemWatcher."
Your objective is to handle as few as necessary to get the job done.
Otherwise, you may have one event handler trying to perform the same IO
operation as another "at the sem time," and run into beaucoups trouble.
So, here's what I want you to do. Handle *only* the Created event. Don't
worry about NotifyFilter. For the OnChange event handler, the default
(LastWrite | FileName | DirectoryName) is perfect. In your OnChange
override, simply move the file specified in the FileSystemEventArgs.FullPath
member passed. The OnChange event handler method will be called for *each*
file dropped into the folder. Remember, *one* at a time!
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
<jimmyfishbean@xxxxxxxxxxx> wrote in message
news:1139920893.901524.19210@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Kevin,
I am monitoring just for the OnCreated event as you have suggested:
fsw.IncludeSubdirectories = False
fsw.NotifyFilter = System.IO.NotifyFilters.LastWrite
fsw.NotifyFilter = System.IO.NotifyFilters.FileName
fsw.NotifyFilter = System.IO.NotifyFilters.LastAccess
AddHandler fsw.Changed, AddressOf OnCreated
AddHandler fsw.Created, AddressOf OnCreated 'not recommended see the
link at top this while loop
In my OnCreated sub, I try to move all files that have been dropped
into the DROP folder to a different folder where they will be
processed. I would expect all files to be moved:
If Not Directory.Exists(strDir & "\Temp") Then
Directory.CreateDirectory(strDir & "\Temp")
End If
If File.Exists(strDir & "\Temp\" & e.Name) Then
File.Delete(strDir & "\Temp\" & e.Name)
End If
'now move the file(s)
For Each oFile In fso.GetFolder(strDir).Files
oFile.Move(strDir & "\Temp\" & e.Name)
bFileMoved = True
Next
However, only one file gets moved to the Temp folder. It seems as if
the OnCreated event gets called when the first file is identified as
being created, and when I try to move all files in the folder, the
filesystem thinks there is only the one file (i.e. the one that
triggered the event). Therefore, I have had to implement a timer that
will read the DROP folder, and move the remaining files periodically.
To me this is defeating the objective of the FileSystemWatcher.
To make matters worse ;P, the memory/no of handles used by my
application is forever increasing. I am closing objects and setting
them to nothing, as well as calling GC.Collect() in each sub.
Is this really how the FileSystemWatcher works? Can you please suggest
a more elegant/efficient way of achieving my aim? Much appreciated.
Jimmy
.
- References:
- FileSystemWatcher advice required please
- From: jimmyfishbean
- Re: FileSystemWatcher advice required please
- From: Kevin Spencer
- Re: FileSystemWatcher advice required please
- From: jimmyfishbean
- Re: FileSystemWatcher advice required please
- From: Kevin Spencer
- Re: FileSystemWatcher advice required please
- From: jimmyfishbean
- FileSystemWatcher advice required please
- Prev by Date: Re: FTPWebRequest
- Next by Date: FTPWebRequest Fails on Some FTP Sites
- Previous by thread: Re: FileSystemWatcher advice required please
- Next by thread: Re: FileSystemWatcher advice required please
- Index(es):
Relevant Pages
|
Loading