Skip to content

Commit

Permalink
EditNetSpell: Avoid ObjectDisposedException (#11160)
Browse files Browse the repository at this point in the history
and remove unnecessary LINQ calls
  • Loading branch information
mstv committed Aug 21, 2023
1 parent 027e83a commit 7e00ab2
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions GitUI/SpellChecker/EditNetSpell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,16 @@ private void ToggleAutoCompletion()
}

InitializeAutoCompleteWordsTask();
CancellationToken cancellationToken = _autoCompleteCancellationTokenSource.Token;

Validates.NotNull(_autoCompleteListTask);
Validates.NotNull(_spelling);

ThreadHelper.JoinableTaskFactory.RunAsync(
async () =>
{
var words = await _autoCompleteListTask.GetValueAsync();
await this.SwitchToMainThreadAsync(_autoCompleteCancellationTokenSource.Token);
IEnumerable<AutoCompleteWord> words = await _autoCompleteListTask.GetValueAsync(cancellationToken);
await this.SwitchToMainThreadAsync(cancellationToken);
_spelling.AddAutoCompleteWords(words.Select(x => x.Word));
});
Expand Down Expand Up @@ -887,16 +888,17 @@ private void InitializeAutoCompleteWordsTask()
{
CancelAutoComplete();
_autoCompleteCancellationTokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = _autoCompleteCancellationTokenSource.Token;
_autoCompleteListTask = new AsyncLazy<IEnumerable<AutoCompleteWord>?>(
async () =>
{
await TaskScheduler.Default.SwitchTo(alwaysYield: true);
var subTasks = _autoCompleteProviders.Select(p => p.GetAutoCompleteWordsAsync(_autoCompleteCancellationTokenSource.Token)).ToArray();
Task<IEnumerable<AutoCompleteWord>>[] subTasks = _autoCompleteProviders.Select(p => p.GetAutoCompleteWordsAsync(cancellationToken)).ToArray();
try
{
var results = await Task.WhenAll(subTasks);
return results.SelectMany(result => result).Distinct().ToList();
IEnumerable<AutoCompleteWord>[] results = await Task.WhenAll(subTasks);
return results.SelectMany(result => result).Distinct();
}
catch (OperationCanceledException)
{
Expand Down Expand Up @@ -1016,8 +1018,8 @@ private void UpdateOrShowAutoComplete(bool calledByUser)
return;
}

var autoCompleteList = ThreadHelper.JoinableTaskFactory.Run(() => _autoCompleteListTask.GetValueAsync());
var list = autoCompleteList.Where(x => x.Matches(word)).Distinct().ToList();
IEnumerable<AutoCompleteWord> autoCompleteList = ThreadHelper.JoinableTaskFactory.Run(_autoCompleteListTask.GetValueAsync);
IReadOnlyList<AutoCompleteWord> list = autoCompleteList.Where(x => x.Matches(word)).ToList();

if (list.Count == 0)
{
Expand Down

0 comments on commit 7e00ab2

Please sign in to comment.