Skip to content

Commit fde6781

Browse files
committed
Cancel previous string search when a new one is requested
1 parent e31aa0f commit fde6781

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Extensions/dnSpy.StringSearcher/StringReferencesService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ You should have received a copy of the GNU General Public License
2121
using System.Collections.Generic;
2222
using System.ComponentModel.Composition;
2323
using System.Linq;
24+
using System.Threading;
2425
using System.Threading.Tasks;
2526
using System.Windows;
2627
using System.Windows.Controls;
@@ -63,6 +64,7 @@ sealed class StringReferencesService : IStringReferencesService {
6364
private readonly StringsControlVM vm;
6465
private readonly Dispatcher dispatcher;
6566
private ModuleDef[] selectedModules = [];
67+
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
6668

6769
public StringsControl UIObject { get; }
6870

@@ -147,12 +149,15 @@ private void AnalyzeSelectedModules() {
147149
dotNetImageService
148150
);
149151

152+
cancellationTokenSource.Cancel();
153+
cancellationTokenSource = new CancellationTokenSource();
154+
150155
vm.StringLiterals.Clear();
151156

152157
Task.Factory.StartNew(() => {
153158
AnalyzeMetadataRoots(context);
154159
AnalyzeModules(context);
155-
});
160+
}, cancellationTokenSource.Token);
156161
}
157162

158163
private void AnalyzeMetadataRoots(StringReferenceContext context) {
@@ -183,7 +188,10 @@ private void AnalyzeMetadataRoots(StringReferenceContext context) {
183188
}
184189

185190
private void AnalyzeModules(StringReferenceContext context) {
186-
Parallel.ForEach(selectedModules.SelectMany(x => x.GetTypes()), type => {
191+
var parallelOptions = new ParallelOptions {
192+
CancellationToken = cancellationTokenSource.Token,
193+
};
194+
Parallel.ForEach(selectedModules.SelectMany(x => x.GetTypes()), parallelOptions, type => {
187195
var typeContext = new ObjectContext {
188196
Context = context,
189197
Module = type.Module,

0 commit comments

Comments
 (0)