private async Task CleanupSearchOperationIfNeeded()
        {
            if (searchOperation == IntPtr.Zero)
            {
                return;
            }

            var operation = searchOperation;

            searchOperation = IntPtr.Zero;

            await Task.Run(() =>
            {
                SearchUtils.CleanupSearchOperation(operation);
            });
        }
        public SearchResultWindow(SearchViewModel searchViewModel)
        {
            InitializeComponent();

            Title       = string.Format("Results for \"{0}\" - FileSystemSearch", searchViewModel.SearchString);
            DataContext = resultsViewModel = new SearchResultsViewModel();
            headerStackPanel.DataContext = searchViewModel;

            progressBar.IsEnabled       = true;
            progressBar.Value           = 0;
            progressBar.IsIndeterminate = true;

            foundPathCallback             = resultsView.AddItem;
            searchProgressUpdatedCallback = OnProgressUpdated;
            searchDoneCallback            = OnSearchDone;

            searchOperation = SearchUtils.SearchAsync(searchViewModel, foundPathCallback, searchProgressUpdatedCallback, searchDoneCallback);
            windowGCHandle  = GCHandle.Alloc(this);
        }
        private void ButtonSearch_Click(object sender, RoutedEventArgs e)
        {
            // This is needed because WPF bindings get only updated when textboxes lose focus
            // If this isn't done, then hitting search button with ENTER shortcut makes us not see the last textbox edit
            ((Button)sender).Focus();

            if (HasValidationErrors(this))
            {
                MessageBox.Show("One or more of search parameters are invalid.", "Search is not possible", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            string validationFailedReason;

            if (!SearchUtils.ValidateSearchViewModel(viewModel, out validationFailedReason))
            {
                MessageBox.Show(validationFailedReason, "Search is not possible", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var resultWindow = new SearchResultWindow(viewModel);

            resultWindow.Show();
        }