C# Events and Thread Safety

前端 未结 15 1129
甜味超标
甜味超标 2020-11-22 06:00

UPDATE

As of C# 6, the answer to this question is:

SomeEvent?.Invoke(this, e);

I frequently hear/read the fo

15条回答
  •  名媛妹妹
    2020-11-22 06:24

    This practice is not about enforcing a certain order of operations. It's actually about avoiding a null reference exception.

    The reasoning behind people caring about the null reference exception and not the race condition would require some deep psychological research. I think it has something to do with the fact that fixing the null reference problem is much easier. Once that is fixed, they hang a big "Mission Accomplished" banner on their code and unzip their flight suit.

    Note: fixing the race condition probably involves using a synchronous flag track whether the handler should run

提交回复
热议问题