[Mono-list] I must misunderstand delegates

Mike Welham mike@digitalnova.co.za
Sat, 20 Nov 2004 16:00:05 +0200


Sorry, I did a really fantastic job of typing that last bit of code :) What
I meant follows...

> One more (pedantic) thing, if your events are intended to be thread-safe,
> they shouldn't be fired as you have done above.
> 
> The problem is that between your null-check and the invocation the
> subscribers could unsubscribe from another thread, and you'll get a
> NullReferenceException thrown at the invocation.
> 
> This can be avoided by doing something like this:
> 
> AgeChangeHandler ageChange = ageChange;
> if (ageChange != null)
> {
>   _ageChange(_age);
> }

AgeChangeHandler ageChange = _ageChange;
if (ageChange != null)
{
  ageChange(_age);
}