namespace Investigator
{
    public static class DirLoader
    {
        static string currentPath = "";
        public static WrapPanel Stillage;
        public static StackPanel PathButtons;
        static CancellationTokenSource cancelSource;

        public static async Task LoadDir(string path)
        {
            cancelSource?.Cancel();
            cancelSource = new CancellationTokenSource();
            currentPath = path;
            Stillage.Children.Clear();
            PathButtons.Children.Clear();
            StepsCreate();
            await LoadAsync();
        }

        static async Task LoadAsync()
        {
            string[] files = await Task.Run(() => Directory.GetFiles(currentPath));
            string[] dirs = await Task.Run(() => Directory.GetDirectories(currentPath));

            List<Task> buttonCreationTasks = new List<Task>();

            foreach (var dir in dirs)
            {
                buttonCreationTasks.Add(ButtonCreateAsync(dir, true, cancelSource.Token));
            }

            foreach (var file in files)
            {
                buttonCreationTasks.Add(ButtonCreateAsync(file, false, cancelSource.Token));
            }

            try
            {
                await Task.WhenAll(buttonCreationTasks);
            }
            catch (OperationCanceledException) { }
        }

        private static async Task ButtonCreateAsync(string path, bool isFolder, CancellationToken cancel)
        {
            try
            {
                cancel.ThrowIfCancellationRequested();
                SolidColorBrush color = Brushes.White;
                if (isFolder) color = (SolidColorBrush)new BrushConverter().ConvertFromString("#DB8");

                Button button = new Button
                {
                    Content = System.IO.Path.GetFileName(path),
                    Margin = new Thickness(5),
                    Padding = new Thickness(5),
                    Background = color
                };

                // Создаем иконку асинхронно
                if (new string[] { ".png", ".jpg", ".dng" }.Contains(System.IO.Path.GetExtension(path).ToLower()))
                {
                    await IconCreator.Create(button, path); // Изменено на асинхронный вызов
                }

                if (isFolder) button.Click += (sender, e) => LoadDir(path);
                else button.Click += (sender, e) => ButtonClick(path);
                // Добавление кнопки в UI поток
                Application.Current.Dispatcher.Invoke(() => Stillage.Children.Add(button));
            }
            catch (OperationCanceledException) { }
        }

        private static void ButtonClick(string path)
        {
            if (File.Exists(path))
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = path,
                    UseShellExecute = true
                });
            }
        }

        private static void StepsCreate()
        {
            bool notFirst = false;
            foreach (string dir in PathSplit(currentPath))
            {
                string folderName = dir;
                if (notFirst) folderName = System.IO.Path.GetFileName(dir);
                Button button = new Button
                {
                    Content = folderName,
                    Margin = new Thickness(3),
                    Padding = new Thickness(5)
                };

                button.Click += (sender, e) => LoadDir(dir);

                // Добавление кнопки в UI поток
                Application.Current.Dispatcher.Invoke(() => PathButtons.Children.Add(button));
                notFirst = true;
            }
        }

        private static string[] PathSplit(string path)
        {
            List<string> list = new List<string>();
            while (!string.IsNullOrEmpty(path))
            {
                list.Add(path);
                path = System.IO.Path.GetDirectoryName(path);
            }
            list.Reverse();
            return list.ToArray();
        }
    }
}
Почему в этом коде не работает прерывание создания кнопок при переходе в новую директорию?