简体   繁体   中英

How individual modules/views behave in Prism (composite application pattern for WPF)

This may come off as a totally n00b question as I know very little about PRISM at all yet, but let's imagine I have a hypothetical situation of a composite application consisting of 3 controls: Control A (a chart), Control B (a table) and Control C (a calculator).

Are all of these controls running on the same UI thread? For example if Control A starting doing some crazy calculation on it's main thread and was blocking - would the entire container freeze?

If the answer to this question is yes - is the only solution to tell the "control team" for A not to do that? Or is there some design pattern we could think about to handle it?

If the answer to this question is no - can you explain a bit how this works or point me at some documentation I could review on the subject?

Thanks

WPF uses a Dispatcher thread to synchronize UI access, so yes essentually they would all be running on the same thread. You can still implement asynchronous calls in the usual way, but to update the UI you need to rejoin the Dispatcher thread using the Dispatcher:

if (!Dispatcher.CheckAccess())
   Dispatcher.Invoke(new Action(() => item.Items.Add(subitem)));
else
   item.Items.Add(subitem);

You can also do this when using the EventAggregator to subscribe to events as follows:

 eventAggregator.GetEvent<AnEvent>().Subscribe(DoWork, ThreadOption.UIThread);

There's some more info here on the subject:

http://msdn.microsoft.com/en-us/library/ms741870.aspx#threading_overview

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