Skip to content

Commit 4edbcdf

Browse files
committed
Simplified compactor logic, removed deep try/catch loop and ensured cancellation tokens are passed through properly
1 parent 099119d commit 4edbcdf

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

CompactGUI.Core/Compactor.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,29 @@ public async Task<bool> RunAsync(List<string> filesList, IProgress<CompressionPr
5353
totalProcessedBytes = 0;
5454

5555
if(maxParallelism <= 0) maxParallelism = Environment.ProcessorCount;
56-
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = maxParallelism };
56+
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = maxParallelism, CancellationToken = cancellationTokenSource.Token };
5757

5858
try
5959
{
6060
await Parallel.ForEachAsync(workingFiles, parallelOptions,
6161
(file, ctx) =>
6262
{
6363
ctx.ThrowIfCancellationRequested();
64+
6465
return new ValueTask(PauseAndProcessFile(file, totalFilesSize, cancellationTokenSource.Token, progressMonitor));
6566
}).ConfigureAwait(false);
6667
}
67-
catch (Exception)
68-
{
68+
catch (OperationCanceledException){ return false; }
69+
catch (Exception){ return false; }
6970

70-
return false;
71-
}
7271
return true;
7372
}
7473

7574
private async Task PauseAndProcessFile(FileDetails file, long totalFilesSize, CancellationToken token, IProgress<CompressionProgress> progressMonitor)
7675
{
77-
try
78-
{
79-
await pauseSemaphore.WaitAsync(token).ConfigureAwait(false);
80-
pauseSemaphore.Release();
81-
}
82-
catch (Exception)
83-
{
84-
throw;
85-
}
8676

87-
token.ThrowIfCancellationRequested();
77+
await pauseSemaphore.WaitAsync(token).ConfigureAwait(false);
78+
pauseSemaphore.Release();
8879

8980
var res = WOFCompressFile(file.FileName);
9081
Interlocked.Add(ref totalProcessedBytes, file.UncompressedSize);

0 commit comments

Comments
 (0)