简体   繁体   中英

Background worker is updating model in UI thread without issue

I'm using a BackgroundWorker and ive implemented ProgressChanged method which gives me e.UserState to pass to my UI, which works OK.

As an experiment I changed my DoWork Method to work on the same model that is bound to the UI ListView (which is an Observable Collection)

The UI updates when I do this, & im wondering why it manages to do it flawlessly after many tests?

Im going back to using the e.UserState object but im wondering if it is normal behaviour & whats the potential problems?

You can certainly change your model's properties from a background thread without any trouble; the binding engine will determine what changes need to be made to UI elements and invoke those on the dispatcher thread¹. This is normal and will not result in problems.

What you cannot (and never could) do is directly change properties of UI elements from the same background thread.


¹This has always been possible, and in fact it couldn't be any other way. Consider that some code (eg a model) needs to change a property on another model. This code surely cannot know that the property has been bound to any control, much less which control that is. Therefore it would not only be inconvenient but also horribly difficult to marshal the property change off to the appropriate UI thread each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM