Changed accessible-directory to cancellable TPL

This commit is contained in:
Ray 2021-12-05 19:35:45 +00:00
parent 5b9a6d4633
commit 68c929b7cb

View File

@ -3,19 +3,34 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RyzStudio.IO
{
public class AccessibleDirectory
{
public static List<string> GetFiles(string path, string pattern, bool searchTopOnly)
public static List<string> GetFiles(string path, string pattern, bool searchTopOnly) => GetFilesAsync(path, pattern, searchTopOnly).Result;
public static Task<List<string>> GetFilesAsync(string path, string pattern, bool searchTopOnly)
{
CancellationToken cancellationToken = new CancellationToken();
return GetFilesAsync(path, pattern, searchTopOnly, cancellationToken);
}
public static Task<List<string>> GetFilesAsync(string path, string pattern, bool searchTopOnly, CancellationToken cancellationToken)
{
return Task.Run<List<string>>(() =>
{
List<string> fileList = new List<string>();
List<string> directoryList = new List<string>();
if (string.IsNullOrWhiteSpace(pattern)) pattern = "*";
if (cancellationToken.IsCancellationRequested) return fileList;
directoryList.Add(path);
while (true)
@ -25,6 +40,8 @@ namespace RyzStudio.IO
break;
}
if (cancellationToken.IsCancellationRequested) return fileList;
string directory = directoryList.First();
directoryList.RemoveAt(0);
@ -32,6 +49,8 @@ namespace RyzStudio.IO
{
IEnumerable<string> searchDirList = new List<string>();
if (cancellationToken.IsCancellationRequested) return fileList;
try
{
searchDirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly);
@ -56,6 +75,8 @@ namespace RyzStudio.IO
foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly))
{
if (cancellationToken.IsCancellationRequested) return fileList;
if (!MatchFileSearchPattern(pattern, Path.GetFileName(item)))
{
continue;
@ -67,11 +88,16 @@ namespace RyzStudio.IO
}
fileList.Add(item);
if (cancellationToken.IsCancellationRequested) return fileList;
}
if (cancellationToken.IsCancellationRequested) return fileList;
}
}
return fileList;
});
}
public static IEnumerable<string> EnumerateFiles(string path, string pattern, bool searchTopOnly)
@ -136,13 +162,26 @@ namespace RyzStudio.IO
}
}
public static void GetFiles(string path, string pattern, bool searchTopOnly, Func<string, ulong, int, bool> callback)
public static void GetFiles(string path, string pattern, bool searchTopOnly, Func<string, ulong, int, bool> callback) => GetFilesAsync(path, pattern, searchTopOnly, callback);
public static Task GetFilesAsync(string path, string pattern, bool searchTopOnly, Func<string, ulong, int, bool> callback)
{
CancellationToken cancellationToken = new CancellationToken();
return GetFilesAsync(path, pattern, searchTopOnly, callback, cancellationToken);
}
public static Task GetFilesAsync(string path, string pattern, bool searchTopOnly, Func<string, ulong, int, bool> callback, CancellationToken cancellationToken)
{
return Task.Run(() =>
{
List<string> directoryList = new List<string>();
ulong searchCount = 0;
if (string.IsNullOrWhiteSpace(pattern)) pattern = "*";
if (cancellationToken.IsCancellationRequested) return;
directoryList.Add(path);
searchCount++;
@ -153,6 +192,8 @@ namespace RyzStudio.IO
break;
}
if (cancellationToken.IsCancellationRequested) return;
string directory = directoryList.First();
directoryList.RemoveAt(0);
@ -171,6 +212,8 @@ namespace RyzStudio.IO
continue;
}
if (cancellationToken.IsCancellationRequested) return;
if (!searchTopOnly)
{
foreach (string item in searchDirList)
@ -189,6 +232,8 @@ namespace RyzStudio.IO
foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly))
{
if (cancellationToken.IsCancellationRequested) return;
if (!MatchFileSearchPattern(pattern, Path.GetFileName(item)))
{
continue;
@ -200,12 +245,21 @@ namespace RyzStudio.IO
}
callback(item, searchCount, directoryList.Count);
}
}
}
if (cancellationToken.IsCancellationRequested) return;
}
public static string GetNextFile(string filepath, string pattern)
if (cancellationToken.IsCancellationRequested) return;
}
}
});
}
public static string GetNextFile(string filepath, string pattern) => GetNextFileAsync(filepath, pattern).Result;
public static Task<string> GetNextFileAsync(string filepath, string pattern)
{
return Task.Run(() =>
{
if (string.IsNullOrWhiteSpace(filepath)) return null;
@ -240,9 +294,14 @@ namespace RyzStudio.IO
}
return null;
});
}
public static string GetPreviousFile(string filepath, string pattern)
public static string GetPreviousFile(string filepath, string pattern) => GetPreviousFileAsync(filepath, pattern).Result;
public static Task<string> GetPreviousFileAsync(string filepath, string pattern)
{
return Task.Run(() =>
{
if (string.IsNullOrWhiteSpace(filepath)) return null;
@ -275,6 +334,7 @@ namespace RyzStudio.IO
}
return null;
});
}
public static bool IsAccessible(string path)