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:
- private void cButton_Click(object sender, RoutedEventArgs e)
- {
- interrupt_worker = true;
- //worker.CancelAsync();
- }
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:
- foreach (SearchResult resEnt in mySearcher.FindAll())
- {
- if (interrupt_worker)
- {
- interrupt_worker = false;
- break;
- } //This block works, if cancel async is not working properly, uncomment
- //if (worker.CancellationPending)
- //{
- // e.Cancel = true;
- // e.Result= dt;
- // return;
- //}
-
I know I am doing something wrong, please correct me.
Thanks

Amit GuptaRank #190 · 12.8K reputationPosted Jan 15, 2017, 1:39 AM EST
worker.WorkerSupportsCancellation =true
Check below reference
http://www.wpf-tutorial.com/misc/cancelling-the-backgroundworker/
Hope it helps
Raj Thampi6 reputationPosted Jan 15, 2017, 1:51 AM EST
Raj Thampi6 reputationPosted Jan 17, 2017, 1:34 AM EST
I got it resolved. By design setting e.Cancel=true will close the background worker and no results will be generated.
Please see the following thread for more details (& a sample)