When await ChangeDirectory(path) started, the main thread stops and the UI does not respond until the method runs.
Question 1: why is this happening?
Question 2: how to make the UI update with each new addition to the ObservableCollection?
public ObservableCollection<FoldersVm> Folders { get; } public async void MouseDoubleClick(object sender, MouseButtonEventArgs e) { var listBox = sender as ListBox; var foldersVm = listBox?.SelectedItem as FolderVm; if (foldersVm != null) { var path = foldersVm.FullPath; if (File.GetAttributes(path) == FileAttributes.Directory) { await ChangeDirectory(path); _previousPath = path; } } } private Task ChangeDirectory(string path) { return Task.Run(() => { Application.Current.Dispatcher.BeginInvoke((Action) delegate() { Folders.Clear(); foreach (var dir in Directory.EnumerateDirectories(path)) { var attr = File.GetAttributes(dir); if ((attr & FileAttributes.Hidden) != 0 || (attr & FileAttributes.System) != 0) continue; Folders.Add(new FolderVm(Path.GetFileName(dir), ImagesHelper.GetFolderImage(dir), dir)); } foreach (string file in Directory.EnumerateFiles(path)) { if (Path.GetExtension(file) == ".jpg") Folders.Add(new FolderVm(Path.GetFileName(file), ImagesHelper.GetFrozenImage(file, 200), file)); } }); }); } ImageHelper.GetFolderImage
public static ImageSource GetFolderImage(string path) { var sf = ShellObject.FromParsingName(path) as ShellFolder; if (sf.Thumbnail != null) return sf.Thumbnail.BitmapSource.Clone() as ImageSource; return null; } Update for VladD
The problem was in the GetFolderImage method, solved it like this:
public static ImageSource GetFolderImage(string path) { var sf = ShellObject.FromParsingName(path) as ShellFolder; if (sf.Thumbnail != null) { var a = sf.Thumbnail.BitmapSource; a.Freeze(); return a; } return null; }