Example #1
0
        private void executeSearch()
        {
            var uiContext = SynchronizationContext.Current;

            HashSet <MatcherConstraint> constraints = new HashSet <MatcherConstraint>();

            if (IsConstraintSameNameEnabled)
            {
                constraints.Add(MatcherConstraint.SameName);
            }
            if (IsConstraintSameCreationDateEnabled)
            {
                constraints.Add(MatcherConstraint.SameCreationDate);
            }
            if (IsConstraintSameModifiedDateEnabled)
            {
                constraints.Add(MatcherConstraint.SameModifiedDate);
            }

            float similarityThreshold = float.Parse(SimilarityThreshold);

            SearchOptions searchOptions = new SearchOptions()
            {
                matcherType = MatcherType,
                similarityMatcherThreshold = similarityThreshold,

                matcherConstraints = constraints,
                searchRootDir      = Path.GetFullPath(SearchRootDir),
                searchPattern      = SearchFilePattern,
                searchOnlyTopDir   = SearchOnlyTopDir,
            };

            OnResultStart.Invoke(null);
            CancelSource = new CancellationTokenSource();
            CancellationToken cancel = CancelSource.Token;

            SearchTask = Task <MultiValueDictionary <string, FileInfo> > .Factory.StartNew(() =>
            {
                return(SearchExecutor.match(searchOptions, cancel));
            }, cancel);

            SearchTask.ContinueWith(task =>
            {
                var result   = task.Result;
                CancelSource = null;
                uiContext.Send(_ => OnSearchAction.RaiseCanExecuteChanged(), null);
                if (result == null)
                {
                    // task was canceled
                    SearchStatus       = "Search was canceled.";
                    CurrentSearchState = SearchState.StoppedNoResults;
                    uiContext.Send(_ => OnResultStart.Invoke(null), null);
                }
                else
                {
                    if (task.Result.Count == 0)
                    {
                        SearchStatus = "No duplicates were found!";
                    }
                    else
                    {
                        SearchStatus = "Adding found duplicates...";
                        Thread.Sleep(100);

                        Console.WriteLine("Presenting Results: Started");
                        uiContext.Send(_ => OnResultStart.Invoke(new SearchInfo()
                        {
                            RootDir = searchOptions.searchRootDir,
                        }), null);

                        foreach (KeyValuePair <string, HashSet <FileInfo> > Entry in task.Result)
                        {
                            uiContext.Send(_ => OnResultPart.Invoke(new SearchResultPart()
                            {
                                GroupKey = Entry.Key,
                                Group    = Entry.Value,
                            }), null);
                            Thread.Sleep(15);
                        }
                        Console.WriteLine("Presenting Results: Done");

                        SearchStatus = "Displaying all found groups of duplicates (" + task.Result.Count + ") !";
                    }
                    CurrentSearchState = SearchState.StoppedShowingResults;
                }
            });
        }