小编pfa*_*faz的帖子

EventAggregator,它是线程安全的吗?

这是线程安全的吗?

Prism中的EventAggregator是一个非常简单的类,只有一个方法.当我注意到null检查没有锁定并创建一个新类型以添加​​到private _events集合时,我感到很惊讶.如果两个线程同时为同一类型调用GetEvent(在_events中存在之前),则看起来这将导致集合中的两个条目.

    /// <summary>
    /// Gets the single instance of the event managed by this EventAggregator. Multiple calls to this method with the same <typeparamref name="TEventType"/> returns the same event instance.
    /// </summary>
    /// <typeparam name="TEventType">The type of event to get. This must inherit from <see cref="EventBase"/>.</typeparam>
    /// <returns>A singleton instance of an event object of type <typeparamref name="TEventType"/>.</returns>
    public TEventType GetEvent<TEventType>() where TEventType : EventBase
    {
        TEventType eventInstance = _events.FirstOrDefault(evt => evt.GetType() == typeof(TEventType)) as TEventType;
        if (eventInstance …
Run Code Online (Sandbox Code Playgroud)

silverlight wpf prism eventaggregator

6
推荐指数
1
解决办法
2360
查看次数

标签 统计

eventaggregator ×1

prism ×1

silverlight ×1

wpf ×1