Add delegate to event - thread safety

后端 未结 3 1820
南笙
南笙 2020-11-30 07:43

It is possible to execute the following code from multiple threads simultaneously.

this._sequencer.Completed += OnActivityFinished;

Is it t

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 08:15

    It depends on the implementation of the event, to be honest.

    The field-like events generated by the C# compiler are thread-safe, but if it's a custom event, who knows?

    Note that in a multi-threaded app you should expect a race condition between adding/removing a handler and the event firing... for example, the event may start to fire, you could then unsubscribe, and your handler would still be called after that unsubscription.

提交回复
热议问题