2010-05-28 3 views
1

나는이 책을 읽고 있어요 여기에 붙어 :잘못된 코드 예약

public static class EventArgExtensions { 
    public static void Raise<TEventArgs>(this TEventArgs e, 
     Object sender, ref EventHandler<TEventArgs> eventDelegate) 
     where TEventArgs : EventArgs { 

     // Copy a reference to the delegate field now into a temporary field for thread safety 
     EventHandler<TEventArgs> temp = 
     Interlocked.CompareExchange(ref eventDelegate, null, null); 

     // If any methods registered interest with our event, notify them 
     if (temp ! = null) temp(sender, e); 
    } 
} 

특히은 MSDN 문서를 말한다

public static object CompareExchange(ref object location1, object value, object comparand) 

System.Threading.Interlocked

의 회원

요약 :
두 개체를 비교하여 참조하도록합니다. 평등성을 유지하고 동등한 경우 객체 중 하나를 대체합니다.

매개 변수 :
LOCATION1 : 간 비교와 비교 가능성이 교체 대상 객체입니다.
: 비교 결과가 같으면 대상 객체를 대체하는 객체입니다.
comparand : 위치 1의 개체와 비교되는 개체입니다.

반품 :
location1의 원래 값.

예외 :
System.ArgumentNullException : LOCATION1의 주소가 널 포인터이다.

EventHandler<TEventArgs> temp = 
      Interlocked.CompareExchange(ref eventDelegate, null, null); 

      // If any methods registered interest with our event, notify them 
      if (temp ! = null) temp(sender, e); 

내가 출력 CompareExchange와하지에을 통과 무엇인지 점검 할 필요가 있기 때문에이 잘못된 것을 의미

.

내가 누락 된 항목이 있습니까?

+0

Safari 서적에서 가져 왔습니다. –

답변

8

아니요, 코드는 괜찮습니다. 이것은 사실상 휘발성 읽기를 수행하는 단순한 방법입니다. eventDelegate이 null 인 경우 null을 null로 바꾼 경우에만 null을 반환합니다.

관련 문제