简体   繁体   English

用于目录遍历的多线程与异步

[英]multithreading vs async for directory traverse

I am writing a code to calculate the size of the folder, for now I have this code我正在编写一个代码来计算文件夹的大小,现在我有这个代码

private long CalculateSize(string root)
    {
        long size = 0;
        Stack<string> dirs = new Stack<string>(20);

        if (!System.IO.Directory.Exists(root))
        {
            throw new ArgumentException();
        }
        dirs.Push(root);

        while (dirs.Count > 0)
        {
            string currentDir = dirs.Pop();
            string[] subDirs;
            try
            {
                subDirs = Directory.GetDirectories(currentDir);
            }
            catch (UnauthorizedAccessException)
            {
                continue;
            }
            catch (DirectoryNotFoundException e)
            {
                continue;
            }

            string[] files = null;
            try
            {
                files = Directory.GetFiles(currentDir);
            }

            catch (UnauthorizedAccessException)
            {
                continue;
            }

            catch (DirectoryNotFoundException e)
            {
                continue;
            }
            foreach (string file in files)
            {
                try
                {
                    FileInfo fi = new System.IO.FileInfo(file);
                    size += fi.Length;
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
            }
            foreach (string str in subDirs)
            {
                dirs.Push(str);
            }
        }
        return size;
    }

It works fine, but it is so slow if I provide some big starting directory like "C:" or "C:\Windows".它工作正常,但如果我提供一些大的起始目录,如“C:”或“C:\Windows”,它会很慢。 I had an idea to do something like:我有一个想法做类似的事情:

string[] subDirs;
subDirs = Directory.GetDirectories(currentDir);

And then start a new thread or parallel process or smth like that for each directory in subDirs, but I have no idea what should I use, and how to implement it.然后为 subDirs 中的每个目录启动一个新线程或并行进程或类似的东西,但我不知道我应该使用什么,以及如何实现它。 Also, do I have to do it for each level of subdirectories?另外,我必须为每个级别的子目录都这样做吗? I mean for example I have "C:" it has 3 subdirectories like "Windows", "ProgramFile", and "ProgramFIlex86", so I am starting a multithreading processes for each, and each of these directories have 50 more directories, do I have to start a new process for each?我的意思是,例如我有“C:”,它有 3 个子目录,如“Windows”、“ProgramFile”和“ProgramFIlex86”,所以我为每个子目录启动了一个多线程进程,每个目录都有 50 个以上的目录,是吗?必须为每个人启动一个新流程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM