0

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?

Community
  • 1
  • 1
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243

1 Answers1

1

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194