C#赛事中的比赛条件? [英] Race Conditions in C# Eventing?
问题描述
VB.Net中的
我非常简单地发起一个事件:
RaiseEvent(MyEvent)''Fire& Forget
在C#中看起来像
如果(MyEvent!= null)//检查,如果有人正在倾听
{
MyEvent(); //消防
}
在多线程环境中会发生什么?我检查后的那一刻
MyEvent最后一个听众可能会撤退,我会有一个
NullReferenceException但是,不是吗?
祝你好运,
Karsten Schramm
2006年7月7日08:02:51 -0700,Karsten Schramm写道:
C#中的相同内容看起来像
if(MyEvent!= null)//检查,如果有人正在倾听
{
MyEvent(); //消防
}
在多线程环境中会发生什么?我检查后的那一刻
MyEvent最后一个听众可以撤退,但我会有一个
NullReferenceException,不是吗?
是的。请参阅Jon Skeet关于线程安全事件的文章:
< http://www.yoda.arachsys.com/csharp/threads/lockchoice.shtml>
Hello Karsten,
VB.net中的RaiseEvent不是线程安全的,你可以在MSIL中看到这个因为
因此使用锁定如
SyncLock我
RaiseEvent(MyEvent)
结束SyncLock
但是,注册和取消注册*始终是*线程安全的。
现在关于C#。
问题是调用注册和取消注册事件是线程 - 安全
如果他们是从课外与活动打来的话
KSin VB.Net我非常简单地开展活动:
KS>
KSRaiseEvent(MyEvent)''Fire& Forget
KS>
KST在C#中看起来像
KS>
KSif(MyEvent!= null)//检查,如果有人正在聆听
KS {
KSMyEvent() ; //消防
KS}
KS在多线程环境中会发生什么?我之后的那一刻
KSchecked MyEvent最后一个听众可能会撤退而且我会有一个
KSNullReferenceException但是,我不是吗?
KS> ;
KSBest问候,
KSKarsten Schramm
---
WBR,
Michael Nemtsev ::博客: http://spaces.msn.com/laflour
有时一个人仍忠于某一事业,只因为它的对手不会因为b $ b不再是平淡的。 (c)Friedrich Nietzsche
Michael Nemtsev< ne ***** @ msn.comwrote:
VB.net中的RaiseEvent不是线程安全的,你可以在MSIL中看到这个
因此使用锁定像
SyncLock Me
RaiseEvent(MyEvent)
结束SyncLock
这是一个非常糟糕的主意IMO - 在你举起活动时持一把锁<
可能是灾难性的,因为事件处理程序可能正在做某事
就像调用Control.Invoke,这可能涉及UI线程等待
来获取相同的锁。
但是,注册和取消注册*始终是*线程安全的。
嗯,这完全取决于你的意思。它总是原子的,但是你不一定会看到其他线程的变化。
-
Jon Skeet - < sk *** @ pobox.com>
http:/ /www.pobox.com/~skeet 博客: http:// www .msmvps.com / jon.skeet
如果回复小组,请不要给我发邮件
Hi,
in VB.Net I fire an event very simply:
RaiseEvent(MyEvent) ''Fire&Forget
The same in C# looks like
if(MyEvent != null) //check, if someone''s listening
{
MyEvent(); //Fire
}
What happens in a multithreaded environment? The moment after I checked
MyEvent the last listener could retreat and I would have a
NullReferenceException nonetheless, wouldn''t I?
Best regards,
Karsten Schramm
On 7 Jul 2006 08:02:51 -0700, Karsten Schramm wrote:The same in C# looks like
if(MyEvent != null) //check, if someone''s listening
{
MyEvent(); //Fire
}
What happens in a multithreaded environment? The moment after I checked
MyEvent the last listener could retreat and I would have a
NullReferenceException nonetheless, wouldn''t I?Yes. See Jon Skeet''s article about thread safe events here:
<http://www.yoda.arachsys.com/csharp/threads/lockchoice.shtml>
Hello Karsten,
RaiseEvent in VB.net is not thread-safe, you can see this in MSIL
thus use locking like
SyncLock Me
RaiseEvent(MyEvent)
End SyncLock
But, registration and unregistration is *always* thread-safe.
Now about C#.
The problem is that calls to register and unregister events are thread-safe
if they are called from outside the class with the event
KSin VB.Net I fire an event very simply:
KS>
KSRaiseEvent(MyEvent) ''Fire&Forget
KS>
KSThe same in C# looks like
KS>
KSif(MyEvent != null) //check, if someone''s listening
KS{
KSMyEvent(); //Fire
KS}
KSWhat happens in a multithreaded environment? The moment after I
KSchecked MyEvent the last listener could retreat and I would have a
KSNullReferenceException nonetheless, wouldn''t I?
KS>
KSBest regards,
KSKarsten Schramm
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Michael Nemtsev <ne*****@msn.comwrote:RaiseEvent in VB.net is not thread-safe, you can see this in MSIL
thus use locking like
SyncLock Me
RaiseEvent(MyEvent)
End SyncLockThat''s a really bad idea IMO - holding a lock while you raise an event
could be catastrophic, as the event handler could be doing something
like calling Control.Invoke, which could involve the UI thread waiting
to acquire the same lock.
But, registration and unregistration is *always* thread-safe.Well, it depends on exactly what you mean. It will always be atomic,
but you won''t necessarily see the change from other threads.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
这篇关于C#赛事中的比赛条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!