Skip to content

Commit

Permalink
Multi-threading profile calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
krisdb2009 committed Feb 10, 2020
1 parent 1587756 commit dbd89be
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions SuperGrate/Classes/FileOperations.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Threading;

namespace SuperGrate.IO
{
Expand Down Expand Up @@ -72,14 +73,17 @@ public static double GetFolderSize(string Path)
class GetFolderSize
{
public double Size = 0;
private ReaderWriterLock Lock = new ReaderWriterLock();
private Dictionary<int, double> ThreadsStatus = new Dictionary<int, double>();
public GetFolderSize(string Path)
{
StartWorker(new DirectoryInfo(Path));
while(true)
{
Task.Delay(5000).Wait();
if (Main.Canceled) return;
Task.Delay(10).Wait();
bool done = true;
Lock.AcquireReaderLock(100);
foreach(KeyValuePair<int, double> threadStatus in ThreadsStatus)
{
if(threadStatus.Value == -1)
Expand All @@ -88,7 +92,12 @@ public GetFolderSize(string Path)
break;
}
}
if (done && ThreadsStatus.Count != 0) break;
if (done && ThreadsStatus.Count != 0)
{
Lock.ReleaseReaderLock();
break;
}
Lock.ReleaseReaderLock();
}
foreach (KeyValuePair<int, double> threadStatus in ThreadsStatus)
{
Expand All @@ -99,24 +108,38 @@ private Task StartWorker(DirectoryInfo Directory)
{
return Task.Run(() => {
double folderSize = 0;
Lock.AcquireWriterLock(100);
ThreadsStatus.Add(Task.CurrentId.Value, -1);
if (Directory.Attributes.HasFlag(FileAttributes.ReparsePoint)) return;
Lock.ReleaseWriterLock();
if (Directory.Attributes.HasFlag(FileAttributes.ReparsePoint))
{
Lock.AcquireWriterLock(100);
ThreadsStatus[Task.CurrentId.Value] = 0;
Lock.ReleaseWriterLock();
return;
}
try
{
foreach (DirectoryInfo subDir in Directory.EnumerateDirectories())
{
if (Main.Canceled) return;
StartWorker(subDir);
}
foreach (FileInfo file in Directory.EnumerateFiles())
{
if (Main.Canceled) return;
folderSize += file.Length;
}
Lock.AcquireWriterLock(100);
ThreadsStatus[Task.CurrentId.Value] = folderSize;
Lock.ReleaseWriterLock();
}
catch (Exception e)
{
Logger.Exception(e, "Failed to get folder size!");
Lock.AcquireWriterLock(100);
ThreadsStatus[Task.CurrentId.Value] = 0;
Lock.ReleaseWriterLock();
return;
}
});
Expand Down

0 comments on commit dbd89be

Please sign in to comment.