@@ -86,7 +86,7 @@ private void RegisterResultsUpdatedEvent()
86
86
{
87
87
foreach ( var pair in PluginManager . GetPluginsForInterface < IResultUpdated > ( ) )
88
88
{
89
- var plugin = ( IResultUpdated ) pair . Plugin ;
89
+ var plugin = ( IResultUpdated ) pair . Plugin ;
90
90
plugin . ResultsUpdated += ( s , e ) =>
91
91
{
92
92
Task . Run ( ( ) =>
@@ -113,25 +113,13 @@ private void InitializeKeyCommands()
113
113
}
114
114
} ) ;
115
115
116
- SelectNextItemCommand = new RelayCommand ( _ =>
117
- {
118
- SelectedResults . SelectNextResult ( ) ;
119
- } ) ;
116
+ SelectNextItemCommand = new RelayCommand ( _ => { SelectedResults . SelectNextResult ( ) ; } ) ;
120
117
121
- SelectPrevItemCommand = new RelayCommand ( _ =>
122
- {
123
- SelectedResults . SelectPrevResult ( ) ;
124
- } ) ;
118
+ SelectPrevItemCommand = new RelayCommand ( _ => { SelectedResults . SelectPrevResult ( ) ; } ) ;
125
119
126
- SelectNextPageCommand = new RelayCommand ( _ =>
127
- {
128
- SelectedResults . SelectNextPage ( ) ;
129
- } ) ;
120
+ SelectNextPageCommand = new RelayCommand ( _ => { SelectedResults . SelectNextPage ( ) ; } ) ;
130
121
131
- SelectPrevPageCommand = new RelayCommand ( _ =>
132
- {
133
- SelectedResults . SelectPrevPage ( ) ;
134
- } ) ;
122
+ SelectPrevPageCommand = new RelayCommand ( _ => { SelectedResults . SelectPrevPage ( ) ; } ) ;
135
123
136
124
SelectFirstResultCommand = new RelayCommand ( _ => SelectedResults . SelectFirstResult ( ) ) ;
137
125
@@ -209,6 +197,7 @@ private void InitializeKeyCommands()
209
197
public ResultsViewModel History { get ; private set ; }
210
198
211
199
private string _queryText ;
200
+
212
201
public string QueryText
213
202
{
214
203
get { return _queryText ; }
@@ -229,10 +218,12 @@ public void ChangeQueryText(string queryText)
229
218
QueryTextCursorMovedToEnd = true ;
230
219
QueryText = queryText ;
231
220
}
221
+
232
222
public bool LastQuerySelected { get ; set ; }
233
223
public bool QueryTextCursorMovedToEnd { get ; set ; }
234
224
235
225
private ResultsViewModel _selectedResults ;
226
+
236
227
private ResultsViewModel SelectedResults
237
228
{
238
229
get { return _selectedResults ; }
@@ -264,6 +255,7 @@ private ResultsViewModel SelectedResults
264
255
QueryText = string . Empty ;
265
256
}
266
257
}
258
+
267
259
_selectedResults . Visbility = Visibility . Visible ;
268
260
}
269
261
}
@@ -324,7 +316,7 @@ private void QueryContextMenu()
324
316
var filtered = results . Where
325
317
(
326
318
r => StringMatcher . FuzzySearch ( query , r . Title ) . IsSearchPrecisionScoreMet ( )
327
- || StringMatcher . FuzzySearch ( query , r . SubTitle ) . IsSearchPrecisionScoreMet ( )
319
+ || StringMatcher . FuzzySearch ( query , r . SubTitle ) . IsSearchPrecisionScoreMet ( )
328
320
) . ToList ( ) ;
329
321
ContextMenu . AddResults ( filtered , id ) ;
330
322
}
@@ -351,7 +343,7 @@ private void QueryHistory()
351
343
Title = string . Format ( title , h . Query ) ,
352
344
SubTitle = string . Format ( time , h . ExecutedDateTime ) ,
353
345
IcoPath = "Images\\ history.png" ,
354
- OriginQuery = new Query { RawQuery = h . Query } ,
346
+ OriginQuery = new Query { RawQuery = h . Query } ,
355
347
Action = _ =>
356
348
{
357
349
SelectedResults = Results ;
@@ -397,7 +389,8 @@ private void QueryResults()
397
389
398
390
_lastQuery = query ;
399
391
Task . Delay ( 200 , currentCancellationToken ) . ContinueWith ( _ =>
400
- { // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
392
+ {
393
+ // start the progress bar if query takes more than 200 ms and this is the current running query and it didn't finish yet
401
394
if ( currentUpdateSource == _updateSource && _isQueryRunning )
402
395
{
403
396
ProgressBarVisibility = Visibility . Visible ;
@@ -410,17 +403,14 @@ private void QueryResults()
410
403
// so looping will stop once it was cancelled
411
404
412
405
Task [ ] tasks = new Task [ plugins . Count ] ;
413
- var parallelOptions = new ParallelOptions { CancellationToken = currentCancellationToken } ;
414
406
try
415
407
{
416
- Parallel . For ( 0 , plugins . Count , parallelOptions , i =>
408
+ for ( var i = 0 ; i < plugins . Count ; i ++ )
417
409
{
418
410
if ( ! plugins [ i ] . Metadata . Disabled )
419
- {
420
411
tasks [ i ] = QueryTask ( plugins [ i ] , query , currentCancellationToken ) ;
421
- }
422
412
else tasks [ i ] = Task . CompletedTask ; // Avoid Null
423
- } ) ;
413
+ }
424
414
425
415
// Check the code, WhenAll will translate all type of IEnumerable or Collection to Array, so make an array at first
426
416
await Task . WhenAll ( tasks ) ;
@@ -434,20 +424,25 @@ private void QueryResults()
434
424
// until the end of all querying
435
425
_isQueryRunning = false ;
436
426
if ( ! currentCancellationToken . IsCancellationRequested )
437
- { // update to hidden if this is still the current query
427
+ {
428
+ // update to hidden if this is still the current query
438
429
ProgressBarVisibility = Visibility . Hidden ;
439
430
}
440
431
441
432
// Local Function
442
433
async Task QueryTask ( PluginPair plugin , Query query , CancellationToken token )
443
434
{
435
+ // Since it is wrapped within a Task.Run, the synchronous context is null
436
+ // Task.Yield will force it to run in ThreadPool
437
+ await Task . Yield ( ) ;
438
+
444
439
var results = await PluginManager . QueryForPlugin ( plugin , query , token ) ;
445
440
if ( ! currentCancellationToken . IsCancellationRequested )
446
441
UpdateResultView ( results , plugin . Metadata , query ) ;
447
442
}
448
-
449
- } , currentCancellationToken ) . ContinueWith ( t => Log . Exception ( "|MainViewModel|Plugins Query Exceptions" , t . Exception ) ,
450
- TaskContinuationOptions . OnlyOnFaulted ) ;
443
+ } , currentCancellationToken ) . ContinueWith (
444
+ t => Log . Exception ( "|MainViewModel|Plugins Query Exceptions" , t . Exception ) ,
445
+ TaskContinuationOptions . OnlyOnFaulted ) ;
451
446
}
452
447
}
453
448
else
@@ -514,6 +509,7 @@ private Result ContextMenuTopMost(Result result)
514
509
}
515
510
} ;
516
511
}
512
+
517
513
return menu ;
518
514
}
519
515
@@ -559,6 +555,7 @@ private bool HistorySelected()
559
555
var selected = SelectedResults == History ;
560
556
return selected ;
561
557
}
558
+
562
559
#region Hotkey
563
560
564
561
private void SetHotkey ( string hotkeyStr , EventHandler < HotkeyEventArgs > action )
@@ -577,7 +574,8 @@ private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
577
574
catch ( Exception )
578
575
{
579
576
string errorMsg =
580
- string . Format ( InternationalizationManager . Instance . GetTranslation ( "registerHotkeyFailed" ) , hotkeyStr ) ;
577
+ string . Format ( InternationalizationManager . Instance . GetTranslation ( "registerHotkeyFailed" ) ,
578
+ hotkeyStr ) ;
581
579
MessageBox . Show ( errorMsg ) ;
582
580
}
583
581
}
@@ -627,7 +625,6 @@ private void OnHotkey(object sender, HotkeyEventArgs e)
627
625
{
628
626
if ( ! ShouldIgnoreHotkeys ( ) )
629
627
{
630
-
631
628
if ( _settings . LastQueryMode == LastQueryMode . Empty )
632
629
{
633
630
ChangeQueryText ( string . Empty ) ;
0 commit comments