Refactoring multithreading

1. ThreadPool -> Task
2. fix deadlock
3. remove unnecessory application.dispatcher.invoke
4. enable non-main thread access to results collection
5. Misc
6. part of #412
This commit is contained in:
bao-qian
2016-05-05 21:15:13 +01:00
parent 923f4ed045
commit d536377329
15 changed files with 146 additions and 140 deletions

View File

@@ -4,7 +4,9 @@ using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using Wox.Core.UserSettings;
using Wox.Plugin;
using Wox.Storage;
@@ -16,17 +18,18 @@ namespace Wox.ViewModel
#region Private Fields
private ResultViewModel _selectedResult;
public ResultCollection Results { get; } = new ResultCollection();
public ResultCollection Results { get; }
private Thickness _margin;
private readonly object _resultsUpdateLock = new object();
private Settings _settings;
private TopMostRecord _topMostRecord;
private readonly object _addResultsLock = new object();
private readonly object _collectionLock = new object();
private readonly Settings _settings;
public ResultsViewModel(Settings settings, TopMostRecord topMostRecord)
public ResultsViewModel(Settings settings)
{
_settings = settings;
_topMostRecord = topMostRecord;
Results = new ResultCollection();
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
}
#endregion
@@ -81,11 +84,6 @@ namespace Wox.ViewModel
#region Private Methods
private bool IsTopMostResult(Result result)
{
return _topMostRecord.IsTopMost(result);
}
private int InsertIndexOf(int newScore, IList<ResultViewModel> list)
{
int index = 0;
@@ -181,45 +179,35 @@ namespace Wox.ViewModel
public void RemoveResultsExcept(PluginMetadata metadata)
{
lock (_resultsUpdateLock)
{
Results.RemoveAll(r => r.RawResult.PluginID != metadata.ID);
}
Results.RemoveAll(r => r.RawResult.PluginID != metadata.ID);
}
public void RemoveResultsFor(PluginMetadata metadata)
{
lock (_resultsUpdateLock)
{
Results.RemoveAll(r => r.PluginID == metadata.ID);
}
Results.RemoveAll(r => r.PluginID == metadata.ID);
}
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
public void AddResults(List<Result> newRawResults, string resultId)
{
lock (_resultsUpdateLock)
lock (_addResultsLock)
{
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
// todo use async to do new result calculation
var resultsCopy = Results.ToList();
var oldResults = resultsCopy.Where(r => r.PluginID == resultId).ToList();
// intersection of A (old results) and B (new newResults)
var intersection = oldResults.Intersect(newResults).ToList();
// remove result of relative complement of B in A
foreach (var result in oldResults.Except(intersection))
{
resultsCopy.Remove(result);
}
// update scores
foreach (var result in newResults)
{
if (IsTopMostResult(result.RawResult))
{
result.Score = int.MaxValue;
}
}
// update index for result in intersection of A and B
foreach (var commonResult in intersection)
{
@@ -300,6 +288,7 @@ namespace Wox.ViewModel
ResultViewModel newResult = newItems[i];
if (!oldResult.Equals(newResult))
{
this[i] = newResult;
}
else if (oldResult.Score != newResult.Score)
@@ -308,6 +297,7 @@ namespace Wox.ViewModel
}
}
if (newCount > oldCount)
{
for (int i = oldCount; i < newCount; i++)
@@ -323,7 +313,6 @@ namespace Wox.ViewModel
RemoveAt(removeIndex);
}
}
}
}