将FileSystemWatcher与大文件一起使用 [英] Using FileSystemWatcher with large files

查看:86
本文介绍了将FileSystemWatcher与大文件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FileSystemWatcher设置一个监视文件夹。

当我将一个小文件放入监视文件夹时,我可以

响应.Created事件并使用

其他代码处理文件。但是,如果我尝试将一个大文件复制到监视文件夹中的

,则.Created事件会立即触发

,而不是在文件完成复制时触发。我已经尝试等待一定数量的.Changed

事件将在初始.Created事件后被触发,并且

该方法有效除了一些

文件触发3。更改事件以及其他5,6和7这一事实之外,还有一些事实。

我找不到确定原因的模式对于各种

文件,有不同数量的事件被触发。有没有其他人有此经验或有

任何建议确定每个文件是什么时候?
写完了?我不想在程序流程中使用检查

循环或错误处理,但此时我可能还有




提前致谢,

--JU

解决方案

Josh,


每当我以这种方式使用FileSystemWatcher时,我会在新线程中处理创建的文件

。每个线程都被添加到一个哈希表中,该哈希表由

引用,因此允许我查看我是否已经为后续事件触发的文件创建了一个处理器




在处理器线程中,我独占打开文件并持续

循环直到打开成功。


Martin。


" Josh Usovsky" <乔** @ usovsky.com>在消息中写道

news:0b **************************** @ phx.gbl ... < blockquote class =post_quotes>我正在使用FileSystemWatcher设置一个监视文件夹。
当我将一个小文件放入监视文件夹时,我可以响应.Created事件并处理该文件用其他代码。但是,如果我尝试将大文件复制到监视文件夹中,则会立即触发.Created事件,而不是在文件完成复制时触发。我已经尝试等待一定数量的.Changed
事件在初始的.Created事件之后被触发,并且
该方法有效,除了一些
文件的事实触发器3.更改事件,以及其他5,6和7.
我找不到一个模式来确定为什么有不同数量的事件被触发各种
文件。有没有其他人有此经验或有任何建议确定每个文件何时完成?我不想在程序流程中使用检查循环或错误处理,但此时我可能已经


提前致谢,
--JU



我在使用FileSystemWatcher处理大文件时遇到了类似的问题

这些是FTP的我们每个晚上。

FileSystemWatcher中的事件触发机制真是太好了。虽然不理想,使用轮询循环是我使用的方法,它似乎是解决这个问题的唯一方法。

当FileSystemWatcher触发事件时,立即尝试以

独占模式打开文件。如果抛出异常,请设置计时器,然后尝试在计时器触发时再次打开文件
。一旦文件可用,停止

计时器并处理文件。


更简单的方法是通过
$响应FileSystemWatcher事件b $ b进入一个循环,其中包含一个Thread.Sleep()调用,每次失败后打开文件。
打开文件。这种方法的缺点是你在Sleep()调用期间导致一个

线程池线程被阻塞。尽管如此,Sleep()循环的95%的时间是完成的。它只是更简单。


David


Josh Usovsky <乔** @ usovsky.com>在消息中写道

news:0b **************************** @ phx.gbl ... < blockquote class =post_quotes>我正在使用FileSystemWatcher设置一个监视文件夹。
当我将一个小文件放入监视文件夹时,我可以响应.Created事件并处理该文件用其他代码。但是,如果我尝试将大文件复制到监视文件夹中,则会立即触发.Created事件,而不是在文件完成复制时触发。我已经尝试等待一定数量的.Changed
事件在初始的.Created事件之后被触发,并且
该方法有效,除了一些
文件的事实触发器3.更改事件,以及其他5,6和7.
我找不到一个模式来确定为什么有不同数量的事件被触发各种
文件。有没有其他人有此经验或有任何建议确定每个文件何时完成?我不想在程序流程中使用检查循环或错误处理,但此时我可能已经


提前致谢,
-JU



" David Sworder" < DS ****** @ cts.com>写在

新闻:eD ************** @ TK2MSFTNGP12.phx.gbl:

事件发生机制在FileSystemWatcher只是
flakey。




事件发射系统是Windows,并且它不是

保证是每个文件操作一个事件。


如果您阅读文档,您将找到它所说的位置

通用文件系统操作可能会增加一个

事件和当一个文件从一个目录移动到

另一个目录时,可能会引发一些OnChanged和一些OnCreated和OnDeleted

事件。


Mark


I''m setting up a watched folder using FileSystemWatcher.
When I drop a small file into the watched folder, I can
respond to a .Created event and process the file with
other code. However, if I try copying a large file into
the watched folder, the .Created event is fired off
immediately and not when the file is finished copying. I
have tried waiting for a certain number of .Changed
events to be fired after the initial .Created event, and
that method works greate except for the fact that some
files trigger 3 .Changed events, and others 5, 6, and 7.
I can''t find a pattern to determine why there are a
differing number of events being fired for the various
files. Does anyone else have experience in this or have
any suggestions for determining when each file is
finished being written ? I''d rather not use a checking
loop or error handling for program flow, but I might have
to at this point.

Thanks in advance,
--JU

解决方案

Josh,

Whenever I use a FileSystemWatcher in this way I process the created files
in new threads. Each thread is added to a hashtable that is referenced by
the file name thus allowing me to see if i have already created a processor
for the file whe subsequent events are fired.

Within the processor thread, I open the file exclusively and continuously
loop until the open is sucessful.

Martin.

"Josh Usovsky" <jo**@usovsky.com> wrote in message
news:0b****************************@phx.gbl...

I''m setting up a watched folder using FileSystemWatcher.
When I drop a small file into the watched folder, I can
respond to a .Created event and process the file with
other code. However, if I try copying a large file into
the watched folder, the .Created event is fired off
immediately and not when the file is finished copying. I
have tried waiting for a certain number of .Changed
events to be fired after the initial .Created event, and
that method works greate except for the fact that some
files trigger 3 .Changed events, and others 5, 6, and 7.
I can''t find a pattern to determine why there are a
differing number of events being fired for the various
files. Does anyone else have experience in this or have
any suggestions for determining when each file is
finished being written ? I''d rather not use a checking
loop or error handling for program flow, but I might have
to at this point.

Thanks in advance,
--JU



I''ve had similar problems using FileSystemWatcher to process large files
which are FTP''d to us each night. The event firing mechanism in
FileSystemWatcher is just flakey. While not ideal, using a polling loop is
the method that I use and it seems to be the only way around this problem.
When FileSystemWatcher fires the event, immediately try to open the file in
exclusive mode. If an exception is thrown, set the timer and then try to
open the file again when the timer fires. Once the file is available, stop
the timer and process the file.

A simpler method is to respond to the FileSystemWatcher event by
entering a loop which includes a Thread.Sleep() call after each failure to
open the file. The downside of this approach is that you''re causing a
threadpool thread to block during the Sleep() call. Nevertheless, 95% of the
time the Sleep() loop is the way to go. It''s just simpler.

David

"Josh Usovsky" <jo**@usovsky.com> wrote in message
news:0b****************************@phx.gbl...

I''m setting up a watched folder using FileSystemWatcher.
When I drop a small file into the watched folder, I can
respond to a .Created event and process the file with
other code. However, if I try copying a large file into
the watched folder, the .Created event is fired off
immediately and not when the file is finished copying. I
have tried waiting for a certain number of .Changed
events to be fired after the initial .Created event, and
that method works greate except for the fact that some
files trigger 3 .Changed events, and others 5, 6, and 7.
I can''t find a pattern to determine why there are a
differing number of events being fired for the various
files. Does anyone else have experience in this or have
any suggestions for determining when each file is
finished being written ? I''d rather not use a checking
loop or error handling for program flow, but I might have
to at this point.

Thanks in advance,
--JU



"David Sworder" <ds******@cts.com> wrote in
news:eD**************@TK2MSFTNGP12.phx.gbl:

The event firing mechanism in FileSystemWatcher is just
flakey.



The event firing system is Windows, and it''s not
guaranteed to be one event per file operation.

If you read the documentation, you''ll find where it says
"Common file system operations might raise more than one
event" and "when a file is moved from one directory to
another, several OnChanged and some OnCreated and OnDeleted
events might be raised".

Mark


这篇关于将FileSystemWatcher与大文件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆