Link to home
Start Free TrialLog in
Avatar of tfsln
tfslnFlag for New Zealand

asked on

BackgroundWorker.WorkerSupportsCancellation Property

This is a question of curiosity.

Why does this propery exist? Why not just always support cancellation? It seems like a pointless property.

Kind of like having to mark classes as serializable... why cant they just be that way by default and remove the need to worry about these properties?
ASKER CERTIFIED SOLUTION
Avatar of Hawkvalley1
Hawkvalley1
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aaron Jabamani
Set the WorkerSupportsCancellation property to true if you want the BackgroundWorker to support cancellation. When this property is true, you can call the CancelAsync method to interrupt a background operation.

It is more flexible way to control the logic, if  the tool want the user to cancel the background or he can not cancel the work based on the business needs.
Avatar of sunithnair
sunithnair

I am not sure of the exact reason but one of the reason I can think of is when you create a custom control and distribute it for others to use it and you donot want the users to cancel a running process in your control which might have an impact on your control. You can in this case set the WorkerSupportsCancellation property to false when you distribute your control and make sure the users do not make use of reflection to do something nasty to your control. This is more to do with the flexibility the programming model provides.
Avatar of tfsln

ASKER

Just seems to me that if you didn't want to allow cancellation, you would write your code in such a way that it doesn't attempt to cancel. It would be extremely poor practice to rely on such properties.

Strange thing really... Maybe not really the right kind of question for this site