I am developing an application with Prism 4.1/.NET 4.0 now. I choose MEF as the container. I am trying to show the market trading window in a new UI thread(STA) so that it will not be affected by the main UI changes.
I googled and found this
. It helped me a lot and I can show the window now.
I'd like to implement this in my application with Prism. It's a trading client app that receives information from the server and shows them in datagrids to users.
I've two modules for now. The shell module contains all the network functions and its views show the news to users in the main UI thread (let's call it UI thread A
). The trading module contains the business logic to show real-time data and
trading functions. The views in trading module should be shown in a different STA UI thread (let's call it UI thread B
). which I create following the above blog. The shell module should publish data to the trading module. I plan to implement
the communication via event aggregator.
Here is the problem. When the viewmodels in trading module subscribe the event from the viewmodels in shell, the callback method always runs on UI thread A(the main UI thread), no matter I set the parameter of Subscribe() to ThreadOption.UIThread or ThreadOption.PublisherThread.
Then if I want to insert records to an ObservableCollection<T> property in viewmodel to update the datagrid, it throws exceptions saying that it can't be invoked in a different thread.
The workaround I can figure out is that when the viewmodels of trading module are created, they should save the referrence to the dispather in UI thread B. In that case, updating UI can call dispather.BeginInvoke(new Action(()=>...)) to insert records. I
think it's not good to include a dispather object in viewmodels, which breaks the MVVM pattern.
Any one can help on this? Do you have similar problems? Thank you in advance!