0

I've been using Prism 4.1 and have numerous classes that subscribe to events (usually in their constructor) using the IEventAggregator and the ThreadOption.UIThread option.

I've now upgraded to Prism 6 but when I run my application it falls over on one such line with an InvalidOperationException. The message is:

To use the UIThread option for subscribing, the EventAggregator must be constructed on the UI thread

The call stack shows that the class in question is being resolved by my DI container (Castle Windsor) hence why it's not on the UI thread. However it all worked fine with Prism 4.1 so what's changed?

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152

2 Answers2

1

It turns out it was down to the way my application was starting up. I was using a "Main" style of entry point, but I needed to move it to the App.xaml OnStartup() instead:

Prism EventAggregator Exception - must be constructed on the UI thread

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
0

I doubt it worked before in the sense of "the subscribers were run on the ui thread", rather they were run on a random thread the EventAggregator is created on. In older version of prism, it just didn't care (and ThreadOption.UIThread lied, in a way).

I don't know Castle Windsor that well, but just because being resolved by the DI framework doesn't mean being created on a different thread. To be on the safe side, resolve the EventAggregator once in InitializeModules in the boot strapper, so that it gets created on the ui thread.

With Unity it would look like this:

internal class Bootstrapper : UnityBootstrapper
{
    protected override void InitializeModules()
    {
        Container.Resolve<IEventAggregator>();

        base.InitializeModules();
    }
}
Haukinger
  • 10,420
  • 2
  • 15
  • 28