开发者

Event synchronization breaking change in Visual C# 2010

In the list of Visual C# 2010 Breaking Changes there is an entry on "Event synchronization" which states开发者_JS百科 that you must now create a local copy of a delegate to check for null (before calling it) in order to avoid a race condition. Wasn't this already the "best practice" pattern anyhow?

Does this change make any difference in the StackOverflow discussion on C# Events and Thread Safety?


Well, you didn't have to take a copy if you used exactly the code they'd got there - because it was locking on this. However:

  • locking on this is a bad idea to start with
  • holding a lock while you execute event handlers is generally a bad idea

So code which was already bad practice is now actively broken. Ho hum. The normal implementation of events (which doesn't hold a lock but does copy the variable) isn't changed by this.

See the Chris Burrows blog post about event best practices for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜