BackgroundWorker.ReportProgress Returns Before Event

J

Jonathan Wood

I didn't realize this but it appears that calling the
BackgroundWorker.ReportProgress method from the DoWork event returns before
the ProgressChanged event is handled.

This means that if I pass a detailed status object to the event, it's
possible that the DoWork handler could make changes to this status object
before the ProgressChanged event has reported that status.

Can someone confirm my understanding is correct, and is there a
straight-forward way to block DoWork until the event has been handled?

Thanks.

Jonathan
 
J

Jonathan Wood

Thanks for the detailed reply. I didn't understand it all but you gave me
some things to look into.
All that said, I would suggest that if you're passing a complex data
structure back for the ProgressChanged event, you might want to rethink
that. The point of BackgroundWorker is simply to address the cross-thread
issues for Forms or WPF components.

It's just a class--it's not that complex. I have some code that runs for
days and I want the display to show me the details of what is going on so I
pass an object that contains the various bits of information.

Perhaps it would be worth making a copy of the class object and passing the
copy. Never done that in C# but could always to it member-by-member if
needed.

Let's see... Looks like I'd need to implement IClonable in my class.

Thanks!

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top