1

Can anyone explain any reason why and when should I use PublishOnBackgroundThread instead of PublishOnUIThread.

I cannot find any use cases for usage PublishOnBackgroundThread and I am not sure what method should I use?

Radenko Zec
  • 7,659
  • 6
  • 35
  • 39

1 Answers1

1

It really depends on the type of the message you're publishing.

If you're using the EventAggregator to surface a message from a low laying service back the UI then PublishOnUIThread makes the most sense since you'll be updating the UI when handling the message. The same applies when you're using it to communicate between view models.

Conversely sometimes it get used for view models to publish events that an underlying service is listening to (rather than the view model depending on that service).

That service may perform some expensive work which makes sense to happen on a background thread. Personally I'd have gone in the background service to push that work onto a background thread but different people want different options.

Ultimately the method was included for completeness.

Nigel Sampson
  • 10,549
  • 1
  • 28
  • 31
  • 1
    I myself wonder about this. I have a service (wcf) that publishes several events and my viewmodels listen to them and update the view accordingly. Should I always use PublishOnUIThread in this case? If I PublishOnBackgroundThread and use `Caliburn.Micro.Execute.OnUIThread` method when handling the event, do I gain anything from it or is it the same as publishing on ui threa? – JobaDiniz Apr 24 '15 at 00:30
  • If all you're doing is using bindings to update the UI then it may not matter as Caliburn automatically makes sure property notifications happen on the UI thread. I can't remember the behaviour of OnBackgroundThread of the top of my head but if it spins up a new thread for the publish then it's a net loss. I'd recommend using OnUIThread. – Nigel Sampson Apr 27 '15 at 08:33