Fix data access of UI thread's data while file system watcher is executing

This commit is contained in:
Peter Kirmeier 2023-04-15 22:19:16 +02:00
parent 562a799a46
commit 176e9f896d
2 changed files with 21 additions and 4 deletions

View file

@ -1219,7 +1219,17 @@ namespace SystemTrayMenu.Business
private void WatcherProcessItem(object sender, EventArgs e)
{
if (menus[0] == null || !menus[0].IsLoaded)
bool UseHistory = false;
if (menus[0] == null)
{
UseHistory = true;
}
else
{
menus[0].Dispatcher.Invoke(() => UseHistory = !menus[0].IsLoaded);
}
if (UseHistory)
{
watcherHistory.Add(e);
return;
@ -1305,7 +1315,7 @@ namespace SystemTrayMenu.Business
{
List<ListViewItemData> rowsToRemove = new();
foreach (ListViewItemData item in dgv.Items)
foreach (ListViewItemData item in dgv.ItemsSource)
{
RowData rowData = item.data;
if (rowData.Path == e.FullPath ||
@ -1318,7 +1328,7 @@ namespace SystemTrayMenu.Business
foreach (ListViewItemData rowToRemove in rowsToRemove)
{
dgv.Items.Remove(rowToRemove);
((IEditableCollectionView)dgv.Items).Remove(rowToRemove);
}
}

View file

@ -25,7 +25,14 @@ namespace SystemTrayMenu.Business
GetNetworkRootDirectories(path, ref menuData);
}
else
{
{
if (!Directory.Exists(path))
{
// Happens most likely when a shortcut is pointing to an absent target path
Log.Info($"path:'{path}' does not exist");
return;
}
GetDirectories(worker, path, ref menuData);
GetFiles(worker, path, ref menuData);
}