[PTRun]Integrated lock mechanism for thread-safe Results updates. (#26104)

This commit is contained in:
gokcekantarci
2023-06-06 15:11:09 +03:00
committed by GitHub
parent 0f6ac52f99
commit ddaa348e5a
2 changed files with 37 additions and 23 deletions

View File

@@ -605,6 +605,8 @@ namespace PowerLauncher
} }
private void ClearResults() private void ClearResults()
{
MainViewModel.PerformSafeAction(() =>
{ {
_viewModel.Results.SelectedItem = null; _viewModel.Results.SelectedItem = null;
System.Threading.Tasks.Task.Run(() => System.Threading.Tasks.Task.Run(() =>
@@ -615,6 +617,7 @@ namespace PowerLauncher
_viewModel.Results.Results.NotifyChanges(); _viewModel.Results.Results.NotifyChanges();
})); }));
}); });
});
} }
private void DeselectAllResults() private void DeselectAllResults()

View File

@@ -41,7 +41,7 @@ namespace PowerLauncher.ViewModel
private readonly PowerToysRunSettings _settings; private readonly PowerToysRunSettings _settings;
private readonly QueryHistory _history; private readonly QueryHistory _history;
private readonly UserSelectedRecord _userSelectedRecord; private readonly UserSelectedRecord _userSelectedRecord;
private readonly object _addResultsLock = new object(); private static readonly object _addResultsLock = new object();
private readonly System.Diagnostics.Stopwatch _hotkeyTimer = new System.Diagnostics.Stopwatch(); private readonly System.Diagnostics.Stopwatch _hotkeyTimer = new System.Diagnostics.Stopwatch();
private string _queryTextBeforeLeaveResults; private string _queryTextBeforeLeaveResults;
@@ -735,6 +735,8 @@ namespace PowerLauncher.ViewModel
private void UpdateResultsListViewAfterQuery(string queryText, bool noInitialResults = false, bool isDelayedInvoke = false) private void UpdateResultsListViewAfterQuery(string queryText, bool noInitialResults = false, bool isDelayedInvoke = false)
{ {
Application.Current.Dispatcher.BeginInvoke(new Action(() => Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
lock (_addResultsLock)
{ {
// Using CurrentCultureIgnoreCase since this is user facing // Using CurrentCultureIgnoreCase since this is user facing
if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase)) if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
@@ -758,6 +760,7 @@ namespace PowerLauncher.ViewModel
{ {
Results.Visibility = Visibility.Hidden; Results.Visibility = Visibility.Hidden;
} }
}
})); }));
} }
@@ -1217,5 +1220,13 @@ namespace PowerLauncher.ViewModel
{ {
return _settings.SearchWaitForSlowResults; return _settings.SearchWaitForSlowResults;
} }
public static void PerformSafeAction(Action action)
{
lock (_addResultsLock)
{
action.Invoke();
}
}
} }
} }