C# - Quitting a BackgroundWorker?

Soldato
Joined
26 Aug 2006
Posts
9,726
Location
62.156684,-49.781113
Oh dear, I'm having a blank Monday and I'm struggling, help appreciated please!

So, I've got my "DoWork" method which then calls a series of loops. I have a cancel button, which activates the CancellationPending thread, which I can check in each of the loops and break from them. However, I want to return from the main DoWork method too obviously. Is there a way of doing this without checking the cancellation flag between each of the (large number of) loops that get called.
 
Checking it in side your loops is the best way. The only other way is storing a reference to the background thread and doing Abort() on it which might be dangerous depending on what you are doing.
 
I am checking it inside the loops, then I can break back into the main DoWork() method, but obviously, this continues then onto the next loop. I'd like to break not only out of the first loop, but out of the DoWork, without having to check between the loops.

DoWork
- Loop 1
- Loop 2
- Loop 3
...
- Loop n

:Loop 1
- For 1:100
-- Task
-- Check cancellation, break

Does that make sense?
 
Check inside and outside the loops?

So you check inside the first loop in order to quit that early, then before you hit the second loop check if CancellationPending and if it is then don't do any of the subsequent loops.

[EDIT]Just realised it seems you are asking if there is another way of doing it compared to this?
[EDIT2]If you check for cancellation at the start of every loop and you say cancel while already inside Loop1 then when you move onto Loop2, Loop3, Loop4 etc. they wouldn't actually iterate over because you will break out of them straight away, not sure what sort of overhead this creates though :p
 
Last edited:
Is it not as simple as changing the break keyword to return? As in

if(CancellationPending) return;

?

Whoops, didn't clarify this - each of the loops is inside its own method. So doing a return only returns from the method.

Check inside and outside the loops?

So you check inside the first loop in order to quit that early, then before you hit the second loop check if CancellationPending and if it is then don't do any of the subsequent loops.

[EDIT]Just realised it seems you are asking if there is another way of doing it compared to this?
[EDIT2]If you check for cancellation at the start of every loop and you say cancel while already inside Loop1 then when you move onto Loop2, Loop3, Loop4 etc. they wouldn't actually iterate over because you will break out of them straight away, not sure what sort of overhead this creates though :p

Looks like that's what it will have to be. I'll just suppress the text output to complete the illusion...
 
I'd probably put a try/catch around most of DoWork and just throw an exception when you detect a cancellation (make some custom exception called UserCancelledException or something).
 
I'd probably put a try/catch around most of DoWork and just throw an exception when you detect a cancellation (make some custom exception called UserCancelledException or something).

That's probably the easiest way of achieving this but I don't think it is a good idea to do this because you are essentially throwing an exception in order to check a condition, i.e. the situation isn't really exceptional but rather it is expected.
 
That's probably the easiest way of achieving this but I don't think it is a good idea to do this because you are essentially throwing an exception in order to check a condition, i.e. the situation isn't really exceptional but rather it is expected.
I see your point - but you could probably argue cancelling is exceptional. It's not really something I'd expect to happen every time!
 
Back
Top Bottom