VB.Net And Background Workers


I have a question regarding the RunWorkerCompleted() event of a Background Worker. I have coded the events up and have set the Background Workers as reporting progress and supporting cancellation. Throughout the processing I check for CancellationPending and take the necessary actions (I believe), but on cancellation and the calling of the CancelAsync() the RunWorkerCompleted event gets fired and I was expecting to be able to test e.Cancelled to see if the completion was because of a cancellation request.

This property never seems to get set - the RunWorkerCompleted() event gets called as soon as I click the cancel button, but the property is not set meaning that the cleanup/display code I have in the event does not get fired.

I must have missed something .... any suggestions would be greatly appreciated as I just cannot see the issue.

Steve MarshallIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
Have you set the WorkSupportsCancellation property to True? Check http://emoreau.com/Entries/Articles/2006/12/The-BackgroundWorker-component.aspx
Steve MarshallIT ManagerAuthor Commented:
Hi Eric,

Yes I have - both in Design Mode and then again in code as the BGW is started up (just to make sure!). I know that the property remains set too because as soon as the code calls the CalcelAsync() on the BGW the RunWorkerCompleted event files even though the "work" is only a percentage complete.

I just expected the BGW to indicate that it had been cancelled (I set the e.Cancel to be true as well when a cancel is detected) by a True value being in the e.Cancelled property when checked in the RunWorkedComplete event - but no.

Steve MarshallIT ManagerAuthor Commented:

Just to say that I have found the issue myself. All I needed was an additional check at the foot of my DoWork() event on the BGW so that when the called routines exited after detection the CancellationPending being set, the DoWork() routine then set the e.Cancel property.

Once this was in place, all is well.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve MarshallIT ManagerAuthor Commented:
I found the issue to be a flaw in the code logic in that the DoWork() was not setting the e.Cancel property in all cases.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.