Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

.NET Background Worker, C# reduce code CancellationPending

views
     
TSAnime4000
post Jul 3 2015, 04:10 PM, updated 9y ago

Regular
******
Senior Member
1,917 posts

Joined: Jul 2009
From: /dev/null


Inside my Background worker has multiple for and foreach loop, I had to place this
CODE
if (bgw.CancellationPending)
      return;

all over the place, there are way to break background worker?

I like minimal code
Nando's
post Jul 3 2015, 06:06 PM

Casual
***
Junior Member
429 posts

Joined: Dec 2012
From: Kitchen
Hi ilham wink.gif
TSAnime4000
post Jul 3 2015, 07:43 PM

Regular
******
Senior Member
1,917 posts

Joined: Jul 2009
From: /dev/null


What?
stormrage
post Jul 13 2015, 10:14 AM

Getting Started
**
Junior Member
111 posts

Joined: Jan 2003
Hi,

Not much input can be offered without any code snippet. Checking CancellationPending is the correct way though for me I won't just return/break but to set the e.Cancel to true
so you can use it in RunWorkerCompleted to do the necessary follow up

something like this
CODE

 if (bgw.CancellationPending) {
   e.Cancel = true;
   return/break;
}

bgw.RunWorkerCompleted += (s, e) => {
 if (e.Cancelled){
    .. the follow up action ...
 }
}
               {


 

Change to:
| Lo-Fi Version
0.0115sec    0.09    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 01:02 AM