EventAggregator,它是线程安全的吗?
这是线程安全的吗?
Prism 中的 EventAggregator 是一个非常简单的类,只有一个方法。当我注意到空检查和创建新类型以添加到私有 _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 == null)
{
eventInstance = Activator.CreateInstance<TEventType>();
_events.Add(eventInstance);
}
return eventInstance;
}
Is this thread-safe?
The EventAggregator in Prism is a very simple class with only one method. I was surprised when I noticed that there was no lock around the null check and creation of a new type to add to the private _events collection. If two threads called GetEvent simultaneously for the same type (before it exists in _events) it looks like this would result in two entries in the collection.
/// <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 == null)
{
eventInstance = Activator.CreateInstance<TEventType>();
_events.Add(eventInstance);
}
return eventInstance;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,不是线程安全的。
我将
http://msdn.microsoft.com/en-us/库/system.collections.concurrent.aspx
或者
No, not thread safe.
I would
http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx
or
这取决于“_events”是什么....
NET 4 中有一些很棒的新线程安全类...
查看 http://msdn.microsoft.com/en-我们/library/system.collections.concurrent.aspx
It depends what "_events" is...
There are some awesome new thread-safe classes in .NET 4 ...
Check out http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx
好吧,根据该代码粘贴,我会说不,它不是 100% 线程安全的。
当然,你有源码,所以你自己加锁就可以了。 :)
实际上,根据一般经验,我将整个 CAL 项目包含在我的解决方案中,至少在开始时是这样。它对于调试那些奇怪的区域注册/创建异常有很大帮助......
Well, based on that code paste, I'd say no, it's not 100% thread safe.
Of course, you have the source, so you can just add the lock yourself. :)
Actually, as a general rule of thumb, I include the entire CAL project in my solution, at least at the start. It helps a lot in debugging those odd region registration/creation exceptions...