2017-05-05 1 views
1

델리게이트 또는 이벤트를 호출하는 데 사용할 수있는 ?. 연산자가 경쟁 조건을 피할 수 있습니까?경쟁 조건을 피하십시오? operator

예 : 수동으로 경쟁 조건을 피하기 :

//The event-invoking method that derived classes can override. 
     protected virtual void OnShapeChanged(ShapeEventArgs e) 
     { 
      // Make a temporary copy of the event to avoid possibility of 
      // a race condition if the last subscriber unsubscribes 
      // immediately after the null check and before the event is raised. 
      EventHandler<ShapeEventArgs> handler = ShapeChanged; 
      if (handler != null) 
      { 
       handler(this, e); 
      } 
     } 

소스 : msdn

+0

예를 평가 스레드 안전합니다. https://codeblog.jonskeet.uk/2015/01/30/clean-event-handlers-invocation-with-c-6/ – vcsjones

답변

2

많이와 스레드 안전한 방법으로 대표 를 호출하는 널 조건의 멤버 액세스에 대한 또 다른 용도 적은 코드. 컴파일러가 에 코드를 생성하기 때문에

...

새로운 방법하여 PropertyChanged 한 번만

MSDN Source

+0

thx btw msdn의 예제도 가상 메서드를 재정의하지 않고 작동합니다. 왜 그런지 아십니까? –

+0

@GernotAtStackoverflow 어떤 가상 메서드를 언급하고 있습니까? – MrZander

+0

protected 가상 void OnShapeChanged (ShapeEventArgs e) –

관련 문제