多线程文件搜索C# [英] Multithread file search C#

查看:180
本文介绍了多线程文件搜索C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。现在,我已经做了文件搜索,将搜索我的整个硬盘驱动器和它的作品。下面是做它的两种方法。

I need some help. Right now i have done a file search that will search my entire hard drive and it works. Here are the two methods that does it.

public void SearchFileRecursiveNonMultithreaded()
    {
        //Search files multiple drive

        string[] drives = Environment.GetLogicalDrives();

        foreach (string drive in drives)
        {
            if (GetDriveType(drive).ToString().CompareTo("DRIVE_FIXED") == 0)
            {
                DriveInfo driveInfo = new DriveInfo(drive);

                if (driveInfo.IsReady)
                {
                    System.IO.DirectoryInfo rootDirectory = driveInfo.RootDirectory;
                    RecursiveFileSearch(rootDirectory);
                }
            }
        }
        MessageBox.Show(files.Count.ToString());
    }

    public void RecursiveFileSearch(DirectoryInfo root)
    {
        DirectoryInfo[] subDirectory;
        try
        {
        //private List<FileInfo> files = new List<FileInfo>() is declared above
            files.AddRange(root.GetFiles(searchString.Text, SearchOption.TopDirectoryOnly));
        }
        catch (Exception)
        {
        }

        try
        {
            // Now find all the subdirectories under this directory.
            subDirectory = root.GetDirectories();

            foreach (System.IO.DirectoryInfo dirInfo in subDirectory)
            {
                // Resursive call will be performed for each subdirectory.
                RecursiveFileSearch(dirInfo);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }



现在,我想实现并行搜索使搜索更快。我试了几个程序来得到这个工作。试图用BackgroundWorker的以及线程,但有它的问题,这是非常难以调试知道什么是错了吗?有人可以让我知道要实现一个具有平行搜索方式。步骤将尽我会去找出我自己。提供将大大apperciated任何帮助。

Right now i am trying to implement a parallel search to make the search faster. I tried several procedures to get this to work. Tried to use backgroundworker as well as threads but have problems with it and it is very difficult to debug to know what is wrong ? Can someone let me know the approach to implement a parrallel search. The step will do i will go and figure out on my own. Any help provided will be greatly apperciated.

推荐答案

首先,正如其他人所指出的,这是不可能的使用多线程将加快速度,当您正在搜索只是一个驱动器。绝大多数的时间都花在等待磁盘磁头移动到它需要,而且它只能在一个地方一次。使用多线程这里浪费精力,而且实际上已经让你的程序慢的可能性很高。

First, as somebody else pointed out, it's unlikely that using multiple threads will speed things up when you're searching just one drive. The vast majority of your time is spent waiting for the disk head to move to where it needs to be, and it can only be in one place at a time. Using multiple threads here is wasted effort, and has a high likelihood of actually making your program slower.

其次,你可以只调用的 Directory.EnumerateFiles 。如果要同时搜索多个驱动器,只需启动多个的BackgroundWorker 实例,每个实例使用 EnumerateFiles 来搜索不同的驱动器。

Second, you can simplify your code by just calling Directory.EnumerateFiles. If you want to search multiple drives concurrently, simply start multiple BackgroundWorker instances, each using EnumerateFiles to search a different drive.

请注意,但是, EnumerateFiles 将抛出一个异常(如将您的代码),如果它在整个目录权限运行问题,搜索整个驱动​​器时,这并不少见。如果这是一个问题(和它可能将是),那么你必须写自己的目录搜索。这样一个在回答这个问题

Note, however, that EnumerateFiles will throw an exception (as will your code) if it runs across directory permissions problems, which aren't uncommon when searching an entire drive. If that's a problem (and it likely will be), then you have to write your own directory searcher. One such is in the answer to this question.

这篇关于多线程文件搜索C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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