Raj Thampi

Raj Thampi

  • NA
  • 6
  • 2.1k

Exit from background worker gracefully, returning dataset

Jan 15 2017 5:13 AM
 Hello
I'm a beginner with WPF & C# and currently trying to develop a small application that lists domain computers with WMI details. I have included a cancel button with the app, to abort the background worker and code block is like following:
 
  1. private void cButton_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             interrupt_worker = true;  
  4.             //worker.CancelAsync();  
  5.   
  6.         } 

and my attempts to exit the do worker loop by checking whether cancellation pending is not returning the dataset and exceptions are shown with do worker completed block

 my do worker block code is like:
 
  1. foreach (SearchResult resEnt in mySearcher.FindAll())  
  2.             {  
  3.   
  4.                 if (interrupt_worker)  
  5.                 {  
  6.                     interrupt_worker = false;  
  7.                     break;  
  8.                       
  9.                 } //This block works, if cancel async is not working properly, uncomment  
  10.   
  11.                 //if (worker.CancellationPending)  
  12.                 //{  
  13.                 //    e.Cancel = true;  
  14.                 //    e.Result= dt;  
  15.                 //    return;  
  16.                 //}  
  17.                  

I know I am doing something wrong, please correct me.

 
Thanks
 
 
 

Answers (3)