C
Apparently, some of the processors write off themselves (or another processor, which is unlikely) from the list.The thing is, locking through. lock It only works on other flows, but not on your flow. And the challenge to processors occurs in the same flow where the method is used SendUpdates♪Note that if the locking was accidentally in the same flow, you'd just get an obsession, because the processors' calls are synchronized, which means the code. handl(this,EventArgs.empty); will wait until the processor is finished.You can, for example, do this:public void SendUpdates()
{
List<handler> copy;
lock(_handlers)
copy = _handlers.ToList(); // это сделает копию списка
foreach (var handl in copy)
handl(this, EventArgs.empty);
}
In doing so, the processor, which was released from the event, will not change the list-copy that runs through the cycle.Note that calling someone else's code (e.g., an event processor) under the block is almost always a bad idea, it's a direct way to envy.I recommend reading this article: https://blogs.msdn.microsoft.com/ericlippert/2009/04/29/events-and-races/ ♪ It describes a few more problems with multiple events, in addition to yours.By the way, why do you need a clear list of processors? If you use the modern language version, you can simplify the code and get rid of the headache:public event EventHandler RainStart;
public void SendUpdates()
{
RainStart?.Invoke(this, EventArgs.Empty);
}
Such a challenge is bleak because the signature and the description of the standard event are performed atomarically.