0

I'm creating WPF application to calculate size of directory. It is necessary that the data on the size of the folders and their contents be displayed in runtime. How can I use recursion multithreading? I tried to use the context of synchronization with tasks, but it did not improve performance.

public Node GetFolderInfo(Node node)
        {           
            context = SynchronizationContext.Current;
            var tscan = new Task(() => ScanStart(node, context));
            tscan.Start();           
            return node;            
        }

private void ScanStart(Node node, SynchronizationContext uiContext)
        {
            Debug.WriteLine($"Thread id = {Task.CurrentId}");
            uiContext.Post(Scan, node);
        }

public void Scan(object nodeObject)
        {
            var node = nodeObject as Node;            

            try
            {
                foreach (var folderPath in Directory.GetDirectories(node.FullName))
                {
                    var folder = new DirectoryInfo(folderPath);
                    var newNode = new Node(folder.Name, folder.FullName, TypeNode.Folder, 0, node);
                    node.CountFolders++;
                    node.Children.Add(newNode);
                    var tscan = new Task(() => ScanStart(newNode, context));
                    tscan.Start();
                    //node.Size += newNode.Size;
                    //node.CountFolders += newNode.CountFolders;
                    //node.CountFiles += newNode.CountFiles;
                }

                foreach (var filePath in Directory.GetFiles(node.FullName))
                {
                    var file = new FileInfo(filePath);                    
                    var newNode = new Node(file.Name, file.FullName, TypeNode.File, file.Length, (Node)nodeObject);
                    node.CountFiles++;
                    node.Children.Add(newNode);
                    node.Size += newNode.Size;                   
                }                
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }            
        }
ilyaVlad
  • 31
  • 5
  • 1
    You may want to read this: https://stackoverflow.com/a/29693430/1136211. Use `await Task.Run(...)` and declare your methods as `async Task ...` or `async Task ...` and await their calls. – Clemens Dec 09 '21 at 14:09
  • 3
    Bear in mind that if you have a mechanical HDD, the latency of the head might be higher than the benefit obtained by many cores involved. – Mario Vernari Dec 09 '21 at 14:19
  • Also note that GetDirectories and GetFiles return potentially large arrays, while EnumerateDirectories and EnumerateFiles won't. – Clemens Dec 09 '21 at 15:43
  • 1
    @MarioVernari, It's a bit more complicated than just "seek time" (see https://en.wikipedia.org/wiki/Hard_disk_drive_performance_characteristics), but yes! If the hard drive performance is the bottleneck, then adding more threads won't make it go any faster. – Solomon Slow Dec 09 '21 at 16:04
  • 1
    @SolomonSlow sure it's more complicated. I'm even closer with the Clemens comment, and threads and enumerations make the life harder. All that reasons to think twice before use threads. – Mario Vernari Dec 09 '21 at 16:39

0 Answers0