关于多线程-.Net的问题

发布于 2024-11-03 03:54:29 字数 202 浏览 1 评论 0原文

我们使用多线程侦听器概念和每个要监视的文件夹的专用线程开发了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-11-10 03:54:29

这是我解决这个问题的方法。

首先,使用 FileSystemWatcher 来监视文件夹。当引发 Created 事件(或任何其他相关事件)时,将引发该事件的文件添加到队列中。该队列将包含所有需要处理的文件。

其次,使用生产者-消费者模式来处理添加到队列中的文件。如果您的队列是 BlockingCollection 类型,那么这很容易完成。 BlockingCollection 的好处是它为您抽象了大部分生产者-消费者逻辑。 Take 方法会阻塞,直到至少有一项添加到队列中。您可以使用任意数量的线程来监视此队列。他们会耐心等待,直到项目添加到队列中。

以下是使用这种方法的一些优点。

  • 由于 BlockingCollection 已经是线程安全的,因此实现起来相对容易。
  • 您想要的并行度取决于您创建的用于监视队列的线程数。
  • 处理文件不会干扰将文件添加到队列的 FileSystemWatcher 事件处理程序。
  • 队列充当缓冲区,其内容会随着文件出现的速率与处理这些文件的速率的变化而变化。

Here is how I would approach the problem.

First, use FileSystemWatcher to monitor the folders. When the Created 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 about BlockingCollection is that it abstracts most of the producer-consumer logic for you. The Take 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.

  • It is relatively easy to implement since BlockingCollection is already thread-safe.
  • The amount of parallelism you want is determined by the number of threads you create to monitor the queue.
  • Processing the files will not interfere with the FileSystemWatcher event handlers which are adding the files to the queue.
  • The queue acts as a buffer whose contents will ebb and flow as the rate at which files appear compared to the rate at which those files are processed changes.
倾城月光淡如水﹏ 2024-11-10 03:54:29

我怀疑文件系统监视器和后台工作子系统的组合可能会对您有所帮助。

对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文