关于多线程-.Net的问题
我们使用多线程侦听器概念和每个要监视的文件夹的专用线程开发了一个 vb.net 应用程序。这样我们就可以同时跟踪每个文件夹中的传入文件。
目前,每当我们收到多个大容量文件时,我们都会遇到一个问题,文件的并行处理会因线程概念而延迟或有时会失败。我们需要使用其他一些概念来实现相同的逻辑,以避免多线程。请就此提出您的建议。或者是否可以通过相同的线程概念来实现性能调优?请指导我。
We have developed a vb.net application using multi-threaded listener concept and a dedicated thread for each folder to monitor. With that we simultaneously keep track of the incoming files in each folder.
Currently, we are facing an issue whenever we receive multiple files of bulk volume, parallel processing of the files getting delayed with thread concept or failing sometimes. We need to implement the same logic with some other concept avoiding multi-hreading. Please provide your suggestion on the same. Or is it possible to achieve performance tuning with the same thread concept? Please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我解决这个问题的方法。
首先,使用 FileSystemWatcher 来监视文件夹。当引发 Created 事件(或任何其他相关事件)时,将引发该事件的文件添加到队列中。该队列将包含所有需要处理的文件。
其次,使用生产者-消费者模式来处理添加到队列中的文件。如果您的队列是
BlockingCollection
类型,那么这很容易完成。BlockingCollection
的好处是它为您抽象了大部分生产者-消费者逻辑。Take
方法会阻塞,直到至少有一项添加到队列中。您可以使用任意数量的线程来监视此队列。他们会耐心等待,直到项目添加到队列中。以下是使用这种方法的一些优点。
FileSystemWatcher
事件处理程序。Here is how I would approach the problem.
First, use
FileSystemWatcher
to monitor the folders. When theCreated
event (or any other event of concern) is raised then add the file which caused the event into a queue. This queue will contain all of the files needing to be processed.Second, use the producer-consumer pattern to process the files added to the queue. This is most easily accomplished if your queue is of type
BlockingCollection
. The nice thing aboutBlockingCollection
is that it abstracts most of the producer-consumer logic for you. TheTake
method blocks until at least one item is added to the queue. You can have as many threads as you want monitoring this queue. They will wait patiently until items are added to the queue.Here are a few of the advantages of using this approach.
BlockingCollection
is already thread-safe.FileSystemWatcher
event handlers which are adding the files to the queue.我怀疑文件系统监视器和后台工作子系统的组合可能会对您有所帮助。
对于 FileSystemMonitor,请查看此处
http://www.codeproject.com/KB/files/monitor_all_filesystem。 。
基本上,您可以使用一些 api 来注册自己,以便在特定文件夹更改(新的/更改的/删除的文件)时收到信号
将其与通过 BackGroundWorked 对象在后台执行工作的系统结合起来,您实际上根本不需要直接处理多个线程。 BackgroundWorkers 将自动在单独的线程上旋转,以保持您的 ui 响应。
I suspect what might help you is a combination of File System monitor and a background worker subsystem.
For FileSystemMonitor, check here
http://www.codeproject.com/KB/files/monitor_all_filesystem.aspx
Basically, there are apis you can use to register yourself to be signalled when specific folder change (new/changed/deleted files).
Couple that with a system of performing work in the background via BackGroundWorked objects, and you shouldn't really have to directly deal with multiple threads at all. BackgroundWorkers will automatically be spun up on seperate threads to keep your ui responsive.