[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

@@ -606,14 +606,17 @@ namespace PowerLauncher
private void ClearResults() private void ClearResults()
{ {
_viewModel.Results.SelectedItem = null; MainViewModel.PerformSafeAction(() =>
System.Threading.Tasks.Task.Run(() =>
{ {
Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => _viewModel.Results.SelectedItem = null;
System.Threading.Tasks.Task.Run(() =>
{ {
_viewModel.Results.Clear(); Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
_viewModel.Results.Results.NotifyChanges(); {
})); _viewModel.Results.Clear();
_viewModel.Results.Results.NotifyChanges();
}));
});
}); });
} }

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;
@@ -736,27 +736,30 @@ namespace PowerLauncher.ViewModel
{ {
Application.Current.Dispatcher.BeginInvoke(new Action(() => Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{ {
// Using CurrentCultureIgnoreCase since this is user facing lock (_addResultsLock)
if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
{ {
Results.Results.NotifyChanges(); // Using CurrentCultureIgnoreCase since this is user facing
} if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
if (Results.Results.Count > 0)
{
Results.Visibility = Visibility.Visible;
if (!isDelayedInvoke || noInitialResults)
{ {
Results.SelectedIndex = 0; Results.Results.NotifyChanges();
if (noInitialResults) }
if (Results.Results.Count > 0)
{
Results.Visibility = Visibility.Visible;
if (!isDelayedInvoke || noInitialResults)
{ {
Results.SelectedItem = Results.Results.FirstOrDefault(); Results.SelectedIndex = 0;
if (noInitialResults)
{
Results.SelectedItem = Results.Results.FirstOrDefault();
}
} }
} }
} else
else {
{ 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();
}
}
} }
} }