经过FileSystemWatcher的火灾 - 线程池或专用线程? [英] After FileSystemWatcher fires - Thread Pool or Dedicated thread?

查看:288
本文介绍了经过FileSystemWatcher的火灾 - 线程池或专用线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将实现原型FileSystemWatcher的解决方案。我有一个目录来监视文件创建,并吸干创建的文件,并插入到数据库的任务。大致这将涉及读取和处理6或7,80字符的文本文件显示在150毫秒中所发生的每几秒钟,而很少一个2MB二进制文件也将必须被处理脉冲串的速率。这很可能是一个全天候的过程。



从我所了解的对象FileSystemWatcher的是更好地排队它的事件在一个线程,然后出队/流程他们在另一个线程。在困惑我现在是什么将是不处理的线程的创建更好的机制。我可以看到这些选择是:




  1. 每次我得到一个FSW事件手动创建一个新的线程(是的,我知道。 。愚蠢的架构,但我不得不说的话)。


  2. 在CLR线程池中扔加工每当我得到一个FSW事件


  3. 在启动时,创造了处理一个专门的第二个线程,并使用一个生产者/消费者模型来处理工作。主线程加入队列请求,第二个线程队列中取出它并执行工作。




我向第三趋向方法为优先选择,因为我知道这个工作线程始终会被要求 - 也可能更多,因为我没有感觉对线程池


解决方案<。 / DIV>

如果您的知道的第二个线程将永远是必需的,并且还知道的,你永远需要多个工作线程,然后选择三是足够了。


I am about to implement the archetypal FileSystemWatcher solution. I have a directory to monitor for file creations, and the task of sucking up created files and inserting the into a DB. Roughly this will involve reading and processing 6 or 7, 80 char text files that appear at a rate of 150mS in bursts that occur every couple of seconds, and rarely a 2MB binary file will also have to be processed. This will most likely be a 24/7 process.

From what I have read about the FileSystemWatcher object it is better to enqueue its events in one thread and then dequeue/process them in another thread. The quandary I have right now is what would be the better creation mechanism of the thread that does the processing. The choices I can see are:

  1. Each time I get a FSW event I manually create a new thread (yeah I know .. stupid architecture, but I had to say it).

  2. Throw the processing at the CLR thread pool whenever I get an FSW event

  3. On start up, create a dedicated second thread for the processing and use a producer/consumer model to handle the work. The main thread enqueues the request and the second thread dequeues it and performs the work.

I am tending towards the third method as the preferred one as I know the work thread will always be required - and also probably more so because I have no feel for the thread pool.

解决方案

If you know that the second thread will always be required, and you also know that you'll never need more than one worker thread, then option three is good enough.

这篇关于经过FileSystemWatcher的火灾 - 线程池或专用线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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