diff options
author | Michael Hutchinson <m.j.hutchinson@gmail.com> | 2015-02-08 11:31:56 +0300 |
---|---|---|
committer | Michael Hutchinson <m.j.hutchinson@gmail.com> | 2015-02-08 11:31:56 +0300 |
commit | 1c124233f582672b5184226bdbcaed0e33c62998 (patch) | |
tree | 438c85e48b2266209b1e4e8ba1fb8c7b981497ef /main | |
parent | ab8578c529046e566c00fcb9ae147dedd3544f99 (diff) |
[Refactoring] Make threading a bit safer
It still seems a bit hairy, it's a bunch of using things
that aren't really threadsafe.
Diffstat (limited to 'main')
-rw-r--r-- | main/src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/FindDerivedClassesHandler.cs | 90 |
1 files changed, 41 insertions, 49 deletions
diff --git a/main/src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/FindDerivedClassesHandler.cs b/main/src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/FindDerivedClassesHandler.cs index b4ade2688d..63b5365aeb 100644 --- a/main/src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/FindDerivedClassesHandler.cs +++ b/main/src/addins/MonoDevelop.Refactoring/MonoDevelop.Refactoring/FindDerivedClassesHandler.cs @@ -40,6 +40,7 @@ using Mono.TextEditor; using ICSharpCode.NRefactory.Semantics; using System.Threading.Tasks; using System.Linq; +using System.Collections.Concurrent; namespace MonoDevelop.Refactoring { @@ -68,57 +69,49 @@ namespace MonoDevelop.Refactoring if (sourceProject == null) return; - var projects = ReferenceFinder.GetAllReferencingProjects (solution, sourceProject); - ThreadPool.QueueUserWorkItem (delegate { - using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true)) { - var cache = new Dictionary<string, TextEditorData> (); - var label = member == null - ? GettextCatalog.GetString ("Searching for derived classes in solution...") - : GettextCatalog.GetString ("Searching for derived members in solution..."); - monitor.BeginTask (label, projects.Count); - - Parallel.ForEach (projects, p => { - var comp = TypeSystemService.GetCompilation (p); - if (comp == null) - return; + var compilations = ReferenceFinder.GetAllReferencingProjects (solution, sourceProject) + .Select (TypeSystemService.GetCompilation).Where (c => c != null).ToList (); + + using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true)) { + var label = member == null + ? GettextCatalog.GetString ("Searching for derived classes in solution...") + : GettextCatalog.GetString ("Searching for derived members in solution..."); + monitor.BeginTask (label, compilations.Count); + + Parallel.ForEach (compilations, comp => { + var importedType = comp.Import (cls); + if (importedType == null) { + return; + } - var importedType = comp.Import (cls); - if (importedType == null) { + IMember impMember = null; + if (member != null) { + impMember = comp.Import (member); + if (impMember == null) { return; } + } - IMember impMember = null; - if (member != null) { - impMember = comp.Import (member); - if (impMember == null) { - return; - } - } + foreach (var derivedType in comp.MainAssembly.GetAllTypeDefinitions ()) { + if (!derivedType.IsDerivedFrom (importedType)) + continue; - foreach (var derivedType in comp.MainAssembly.GetAllTypeDefinitions ()) { - if (!derivedType.IsDerivedFrom (importedType)) + IEntity result; + if (member != null) { + result = FindDerivedMember (impMember, derivedType); + if (result == null) continue; - - IEntity result; - if (member != null) { - result = FindDerivedMember (impMember, derivedType); - if (result == null) - continue; - } else { - result = derivedType; - } - - ReportResult (monitor, result, cache); + } else { + result = derivedType; } - monitor.Step (1); - }); - foreach (var tf in cache.Values) { - if (tf.Parent == null) - tf.Dispose (); + + ReportResult (monitor, result); } - monitor.EndTask (); - } - }); + monitor.Step (1); + }); + + monitor.EndTask (); + }; } static IMember FindDerivedMember (IMember importedMember, ITypeDefinition derivedType) @@ -134,23 +127,22 @@ namespace MonoDevelop.Refactoring return derivedMember; } - static void ReportResult (ISearchProgressMonitor monitor, IEntity result, Dictionary<string, TextEditorData> cache) + static void ReportResult (ISearchProgressMonitor monitor, IEntity result) { string filename = result.Region.FileName; if (string.IsNullOrEmpty (filename)) return; - TextEditorData textFile; - if (!cache.TryGetValue (filename, out textFile)) { - cache [filename] = textFile = TextFileProvider.Instance.GetTextEditorData (filename); - } - + var textFile = TextFileProvider.Instance.GetTextEditorData (filename); var start = textFile.LocationToOffset (result.Region.Begin); textFile.SearchRequest.SearchPattern = result.Name; var sr = textFile.SearchForward (start); if (sr != null) start = sr.Offset; + if (textFile.Parent == null) + textFile.Dispose (); + monitor.ReportResult (new MemberReference (result, result.Region, start, result.Name.Length)); } |