public MainWindow() { InitializeComponent(); _fileDiver = new FileDiver(); App.LanguageChanged += LanguageChanged; CultureInfo currentLanguage = App.Language; foreach (var language in App.Languages) { var menuItem = new MenuItem { Header = language.DisplayName, Tag = language, IsChecked = Equals(currentLanguage) }; menuItem.Click += ChangeLanguageClick; LanguageMenu.Items.Add(menuItem); } if (!File.Exists(DbHelper.DbPath)) { DbHelper.CreateDb(); DbHelper.CreateReadedFilesTable(); } try { MainWindowX.WindowStartupLocation = WindowStartupLocation.Manual; _tokenSource = new CancellationTokenSource(); _searchEngine = new SearchEngine(); CbDrives.ItemsSource = Directory.GetLogicalDrives(); CbDrives.SelectedIndex = 0; InitExplorer(); } catch (IOException ioException) { var messageDialog = new MessageDialog(ioException.Message) { WindowStartupLocation = WindowStartupLocation.CenterOwner, Owner = this }; messageDialog.ShowDialog(); } catch (UnauthorizedAccessException unauthorizedAccessException) { var messageDialog = new MessageDialog(unauthorizedAccessException.Message) { WindowStartupLocation = WindowStartupLocation.CenterOwner, Owner = this }; messageDialog.ShowDialog(); } }
private async void BtnSearch_OnClick(object sender, RoutedEventArgs e) { if (_searchTask == null) { await PbVisualization.TogglePbVisibilityAsync(); var stopwatch = Stopwatch.StartNew(); _tokenSource = new CancellationTokenSource(); _searchEngine = new SearchEngine(); string pattern = TbSearchInput.Text; await Dispatcher.InvokeAsync(() => { _searchWindow?.LbSearchResults.Items.Clear(); }); _searchEngine.OnItemFound += async (o, args) => { await Dispatcher.InvokeAsync(() => { if (_searchWindow == null) { _searchWindow = new SearchWindow(_tokenSource); _searchWindow.LbSearchResults.Items.Add(args.FoundedItem); _searchWindow.Closing += (sender1, eventArgs) => { _searchWindow = null; }; _searchWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner; _searchWindow.Owner = this; _searchWindow.Show(); } else { _searchWindow.LbSearchResults.Items.Add(args.FoundedItem); } }); }; _searchTask = new Task(()=> { try { _searchEngine.SearchUsingEnumeration(_fileDiver.CurrentPath, pattern, _tokenSource.Token); } catch(Exception) { Dispatcher.InvokeAsync(() => { SearchPopup.IsOpen = true; SystemSounds.Exclamation.Play(); }); } }, _tokenSource.Token); _searchTask.Start(); try { await _searchTask; stopwatch.Stop(); if (_searchWindow != null) { _searchWindow.Title += $" || {Properties.Resources.MwSearchCompleted} {stopwatch.Elapsed}"; } } catch { if (stopwatch.IsRunning) stopwatch.Stop(); if (_searchWindow != null) { _searchWindow.Title += $" || {Properties.Resources.MwSearchCompleted} {stopwatch.Elapsed}"; } SearchPopup.IsOpen = true; SystemSounds.Exclamation.Play(); } _searchTask = null; await PbVisualization.TogglePbVisibilityAsync(); } else { _tokenSource.Cancel(true); await PbVisualization.TogglePbVisibilityAsync(); } }