Added a done callback

This commit is contained in:
Thorsten Sommer 2025-01-23 13:54:11 +01:00
parent be8fd18975
commit 6ac122bfd8
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -8,7 +8,7 @@ public static class DirectoryInfoExtensions
RecurseSubdirectories = true, RecurseSubdirectories = true,
ReturnSpecialDirectories = false, ReturnSpecialDirectories = false,
}; };
/// <summary> /// <summary>
/// Determines the size of the directory and all its subdirectories, as well as the number of files. When desired, /// Determines the size of the directory and all its subdirectories, as well as the number of files. When desired,
/// it can report the found files up to a certain limit. /// it can report the found files up to a certain limit.
@ -17,20 +17,21 @@ public static class DirectoryInfoExtensions
/// You might set reportMaxFiles to a negative value to report all files. Any positive value will limit the number /// You might set reportMaxFiles to a negative value to report all files. Any positive value will limit the number
/// of reported files. The cancellation token can be used to stop the operation. The cancellation operation is also able /// of reported files. The cancellation token can be used to stop the operation. The cancellation operation is also able
/// to cancel slow operations, e.g., when the directory is on a slow network drive. /// to cancel slow operations, e.g., when the directory is on a slow network drive.
/// ///
/// After stopping the operation, the total size and number of files are reported as they were at the time of cancellation. /// After stopping the operation, the total size and number of files are reported as they were at the time of cancellation.
/// ///
/// Please note that the entire operation is done on a background thread. Thus, when reporting the found files or the /// Please note that the entire operation is done on a background thread. Thus, when reporting the found files or the
/// current total size, you need to use the appropriate dispatcher to update the UI. Usually, you can use the InvokeAsync /// current total size, you need to use the appropriate dispatcher to update the UI. Usually, you can use the InvokeAsync
/// method to update the UI from a background thread. /// method to update the UI from a background thread.
/// </remarks> /// </remarks>
/// <param name="directoryInfo"></param> /// <param name="directoryInfo">The root directory to determine the size of.</param>
/// <param name="reportCurrentTotalSize"></param> /// <param name="reportCurrentTotalSize">The callback to report the current total size of the directory.</param>
/// <param name="reportCurrentNumFiles"></param> /// <param name="reportCurrentNumFiles">The callback to report the current number of files found.</param>
/// <param name="reportNextFile"></param> /// <param name="reportNextFile">The callback to report the next file found. The file name is relative to the root directory.</param>
/// <param name="reportMaxFiles"></param> /// <param name="reportMaxFiles">The maximum number of files to report. A negative value reports all files.</param>
/// <param name="cancellationToken"></param> /// <param name="done">The callback to report that the operation is done.</param>
public static async Task DetermineContentSize(this DirectoryInfo directoryInfo, Action<long> reportCurrentTotalSize, Action<long> reportCurrentNumFiles, Action<string> reportNextFile, int reportMaxFiles = -1, CancellationToken cancellationToken = default) /// <param name="cancellationToken">The cancellation token to stop the operation.</param>
public static async Task DetermineContentSize(this DirectoryInfo directoryInfo, Action<long> reportCurrentTotalSize, Action<long> reportCurrentNumFiles, Action<string> reportNextFile, int reportMaxFiles = -1, Action? done = null, CancellationToken cancellationToken = default)
{ {
var rootDirectoryLen = directoryInfo.FullName.Length; var rootDirectoryLen = directoryInfo.FullName.Length;
long totalSize = 0; long totalSize = 0;
@ -58,5 +59,8 @@ public static class DirectoryInfoExtensions
reportCurrentTotalSize(totalSize); reportCurrentTotalSize(totalSize);
reportCurrentNumFiles(numFiles); reportCurrentNumFiles(numFiles);
if(done is not null)
done();
} }
} }