如何使用TPL搜索文件系统? [英] How to search Filesystem with TPL?

查看:64
本文介绍了如何使用TPL搜索文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试对每条语句使用Task Parallel Library来搜索文件系统而没有运气.我知道我一定做错了什么,或者不了解如何实现多进程线程.

顺便说一句,我看到了一些使用get files方法的有限解决方案,但是我已经放弃了,因为我希望搜索多个文件扩展名.



有人可以提供一个链接或快速示例来说明如何使用此方法迭代文件系统吗?

非常感谢!

每个语句的内容如下:

I have tried to use Task Parallel Library within my for each statement to search the filesystem with no luck. I know that I must be doing something wrong or do not understand how to implement the multiple process threading.

along that being said I have seen some limited solutions that use the get files method but I have moved away from that because I wish to search for more than one file extension.



can someone please give a link or quick example of how to iterate a filesystem using this method?

thank you very much!

the for each statement is as follows:

Try
               Using fse As New FindFiles.FileSystemEnumerator(pathToSearchCombo.Text, fileSpecsCombo.Text, includeSubdirectoriesCheck.Checked)
                   Dim ien As IEnumerator(Of FileInfo) = fse.Matches().GetEnumerator()
                   ien.Dispose()
                   For Each fi As FileInfo In fse.Matches

推荐答案

此处的性能提升.大部分处理时间都在生成列表中.如果仅对每个文件做一些简单的事情,您将不会从TPL中获得任何好处,而只会增加复杂性.但是,如果您要对每个文件进行复杂的处理或占用大量处理器资源,那么TPL确实可以为您提供帮助.

这里的另一个选择是使用TPL搜索一组目录中的每个目录.假设您要在以下目录中搜索所需文件:

  • C:\ Files
  • C:\ Temp
  • C:\ Users \ Tim \ Documents
  • F:\
  • G:\ Data
  • S:\ SavedDocs
I doubt you are going to get the TPL to give you much of a performance gain here. Most of the processing time is involved in generating your list. If you just do something simple with each file you won''t get any benefits from the TPL and you will just add complexity. However, if you were to do something complicated or processor-intensive with each file then the TPL would really help you out.

The other option here would be to use the TPL to search each directory in a set of directories. Say, for example, you wanted to do the search in the following directories for the files you wanted:

  • C:\Files
  • C:\Temp
  • C:\Users\Tim\Documents
  • F:\
  • G:\Data
  • S:\SavedDocs


由于磁盘几乎是串行设备,因此只能提供文件/目录信息或单个磁盘扇区,对于一次只有一个线程,文件系统/磁盘扫描并不能真正起到并行作用.

我知道缓存会在一定程度上使该语句错误.但是,在扫描整个文件系统的情况下不会这样做,因为缓存只会在您再次读取磁盘之前走得很远.
Since the disk is pretty much a serial device, it can only give you the file/directory information, or a single disk sector, for a single thread at a time, file system/disk scanning doesn''t really lend itself to parrellism.

I know caching will make that statement false to a limited degree. But, it will not in the case of scanning an entire file system, as caching will only go so far before you''re reading the disk again.


这篇关于如何使用TPL搜索文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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