tags:

views:

84

answers:

1

In the list of Visual C# 2010 Breaking Changes there is an entry on "Event synchronization" which states 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?

+1  A: 

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.

Jon Skeet