简体   繁体   English

C#事件处理程序行为

[英]C# Event handler Behavior

I have this code 我有这个代码

List<DaSubscription> lstSubscription=new List<DaSubscription>();

for(int i=0;i<20;i++)//20 is just to simulate the behavior
{

    DaSubscription Generic=new DaSubscription();
    Generic.DataChanged += new DataChangedEventHandler(Generic_DataChanged);
    lstSubscription.add(Generic);
}

//EVENT Handler which is raised from a 3rd party library [COM]                 

void Generic_DataChanged(DaSubscription aDaSubscription, DaItem[] items, ValueQT[] values, int[] results)
{
   UpdateDataChangedDTO(items, values);
}

As the same event handler [m_daSubscription_Generic_DataChanged] is assigned to the multiple instance of same class [m_daSubscription]. 由于将同一事件处理程序[m_daSubscription_Generic_DataChanged]分配给同一类[m_daSubscription]的多个实例。 Question i have is if at the same time multiple instances invokes this handler how will be handled here. 我的问题是,如果同时有多个实例调用此处理程序,将在这里如何处理。 will there will be any instance it shall overwrite the data. 在任何情况下都将覆盖数据。 or the event handler will be separate for each instance. 否则每个实例的事件处理程序将是独立的。

The event handlers execute seperately. 事件处理程序是分开执行的。 It sounds like your worried about the parameters being overwritten by another call to the handler. 听起来您担心其他调用处理程序会覆盖参数。 That won't happen (I don't think it is even possible). 那将不会发生(我什至认为这是不可能的)。 Since it doesn't look like you are accessing any shared objects in the event handler, you should be perfectly safe. 由于看起来好像您在事件处理程序中未访问任何共享对象,因此应该绝对安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM