Show progress when searching all files in a directory

后端 未结 2 1076
半阙折子戏
半阙折子戏 2021-02-04 18:51

I previously asked the question Get all files and directories in specific path fast in order to find files as fastest as possible. I am using that solution in order to find the

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 19:18

    Solving this is going to leave you with one of a few possibilities...

    1. Not displaying a progress
    2. Using an up-front cost to compute (like Windows)
    3. Performing the operation while computing the cost

    If the speed is that important and you expect large directory trees I would lean to the last of these options. I've added an answer on the linked question Get all files and directories in specific path fast that demonstrates a faster means of counting files and sizes than you are currently using. To combine this into a multi-threaded piece of code for option #3, the following can be performed...

    static void Main()
    {
        const string directory = @"C:\Program Files";
        // Create an enumeration of the files we will want to process that simply accumulates these values...
        long total = 0;
        var fcounter = new CSharpTest.Net.IO.FindFile(directory, "*", true, true, true);
        fcounter.RaiseOnAccessDenied = false;
        fcounter.FileFound +=
            (o, e) =>
                {
                    if (!e.IsDirectory)
                    {
                        Interlocked.Increment(ref total);
                    }
                };
    
        // Start a high-priority thread to perform the accumulation
        Thread t = new Thread(fcounter.Find)
            {
                IsBackground = true, 
                Priority = ThreadPriority.AboveNormal, 
                Name = "file enum"
            };
        t.Start();
    
        // Allow the accumulator thread to get a head-start on us
        do { Thread.Sleep(100); }
        while (total < 100 && t.IsAlive);
    
        // Now we can process the files normally and update a percentage
        long count = 0, percentage = 0;
        var task = new CSharpTest.Net.IO.FindFile(directory, "*", true, true, true);
        task.RaiseOnAccessDenied = false;
        task.FileFound +=
            (o, e) =>
                {
                    if (!e.IsDirectory)
                    {
                        ProcessFile(e.FullPath);
                        // Update the percentage complete...
                        long progress = ++count * 100 / Interlocked.Read(ref total);
                        if (progress > percentage && progress <= 100)
                        {
                            percentage = progress;
                            Console.WriteLine("{0}% complete.", percentage);
                        }
                    }
                };
    
        task.Find();
    }
    

    The FindFile class implementation can be found at FindFile.cs.

    Depending on how expensive your file-processing task is (the ProcessFile function above) you should see a very clean progression of the progress on large volumes of files. If your file-processing is extremely fast, you may want to increase the lag between the start of enumeration and start of processing.

    The event argument is of type FindFile.FileFoundEventArgs and is a mutable class so be sure you don't keep a reference to the event argument as it's values will change.

    Ideally you will want to add error handling and probably the ability to abort both enumerations. Aborting the enumeration can be done by setting "CancelEnumeration" on the event argument.

提交回复
热议问题