private void SearchBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { SearchUserState progress = (SearchUserState)e.UserState; ShowProgress(progress.results); progress.extension = (String)this.extensionComboBox.GetItemText(this.extensionComboBox.SelectedItem); }
private void Search(String extension) { // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } // Initial results is empty list FileInfo[] searchResults = new FileInfo[] { }; // Report initial progress this.SearchBackgroundWorker.ReportProgress(0, state = new SearchUserState(searchResults, extension)); Debug.WriteLine(state.extension); foreach (String drive in Directory.GetLogicalDrives()) { // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } Debug.WriteLine(drive); foreach (DirectoryInfo child in getDirectories(drive)) { // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } Debug.WriteLine(child.FullName); FindFiles(child, state.extension); } } }
private void FindFiles(DirectoryInfo dir, string selectedItem) { try { if (this.backgroundWorker.CancellationPending) { return; } DirectoryInfo[] children = getDirectories(dir); if (children.Length > 0) { foreach (DirectoryInfo child in children) { Debug.WriteLine(child.FullName); FindFiles(child, selectedItem); } } else { if (selectedItem == ".txt") { FileInfo[] Files = dir.GetFiles("*.txt"); if (Files.Length > 0) { //Found some text files. //Do something //Report progress fileCount++; SearchUserState state = new SearchUserState(Files); this.backgroundWorker.ReportProgress(0, state); } } else if (selectedItem == ".html") { FileInfo[] Files = dir.GetFiles("*.html"); if (Files.Length > 0) { //Found some html files. //Do something //Report progress fileCount++; SearchUserState state = new SearchUserState(Files); this.backgroundWorker.ReportProgress(0, state); } } else if (selectedItem == ".htm") { FileInfo[] Files = dir.GetFiles("*.htm"); if (Files.Length > 0) { //Found some htm files. //Do something //Report progress fileCount++; SearchUserState state = new SearchUserState(Files); this.backgroundWorker.ReportProgress(0, state); } } while (pause) { Thread.Sleep(1); if (this.backgroundWorker.CancellationPending) { return; } } } } catch (Exception ex) { System.Console.WriteLine(ex.Message); } }
private void FindFiles(DirectoryInfo dir, String extension) { // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } try { // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } DirectoryInfo[] children = getDirectories(dir); if (children.Length > 0) { foreach (DirectoryInfo child in children) { // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } Debug.WriteLine(child.FullName); Debug.WriteLine(extension); FindFiles(child, extension); } } else { // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } FileInfo[] Files = dir.GetFiles(extension); if (Files.Length > 0) { //Found some files with the given extension this.SearchBackgroundWorker.ReportProgress(0, state = new SearchUserState(Files, state.extension)); pauseEvent.WaitOne(); Debug.WriteLine(state.extension); // Check for cancellation if (this.SearchBackgroundWorker.CancellationPending) { return; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { SearchUserState progress = (SearchUserState)e.UserState; ShowProgress(progress.Files); //passing data back to ui }