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);
}
}