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
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Raj ThampiPosted Jan 17, 2017, 6:34 AM
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)
Raj ThampiPosted Jan 15, 2017, 6:51 AM
Amit GuptaPosted Jan 15, 2017, 6:39 AM
worker.WorkerSupportsCancellation =true
Check below reference
http://www.wpf-tutorial.com/misc/cancelling-the-backgroundworker/
Hope it helps