8

下面是我的代码,第一部分是我引发事件的地方,第二部分是我在另一个类中使用它的地方。看起来很简单,但日志显示即使事件被引发一次,事件在使用该事件的类上触发了 20 多次。有任何想法吗?

IBSerialPort班级:

public delegate void PacketReceivedHandler(object sender, PacketReceivedEventArgs e);
public event PacketReceivedHandler OnPacketReceived;

public class PacketReceivedEventArgs : EventArgs
{
  public Packet PacketReceived { get; private set; }

  public PacketReceivedEventArgs(Packet packet)
  {
    PacketReceived = packet;
  }
}

// raise event
if (OnPacketReceived != null)
{
    Log("This is only called ONCE!");
    PacketReceivedEventArgs args = new PacketReceivedEventArgs(data);
    OnPacketReceived(this, args);
}

使用IBSerialPort和消费其OnPacketReceived事件的类:

IBSerialPort ibSerialPort = null;
..
if (ibSerialPort == null)
{
  Log("This is only called once");

  ibSerialPort = IBSerialPort.Instance;

  ibSerialPort.OnPacketReceived += ibSerialPort_OnPacketReceived;
}

void ibSerialPort_OnPacketReceived(object sender, IBSerialPort.PacketReceivedEventArgs args)
{
   Log("This is called ~25 times!!!!");
}
4

4 回答 4

10

试试这个,这将取消注册任何 prev 订阅者:

ibSerialPort.OnPacketReceived -= ibSerialPort_OnPacketReceived;   // unregister
ibSerialPort.OnPacketReceived += ibSerialPort_OnPacketReceived;  //register
于 2014-08-07T19:13:00.560 回答
4

这被调用了多少次?如果这被多次调用,那么您的事件将被多次调用。

 ibSerialPort.OnPacketReceived += ibSerialPort_OnPacketReceived;

作为测试,您可以在添加之前删除委托:

ibSerialPort.OnPacketReceived -= ibSerialPort_OnPacketReceived;
ibSerialPort.OnPacketReceived += ibSerialPort_OnPacketReceived;
于 2014-08-07T19:11:44.527 回答
1

我想知道您定义的类是否ibSerialPort_OnPacketReceived被使用(即使在单独的实例中)25 次,并且您认为您正在释放它。考虑这段代码:

class EventSender
{
    public Action MyEvent;
}

class Subscriber
{
    public void OnEvent()
    {
        Console.WriteLine("OnEvent");
    }
}

class Program
{
    static void Main(string[] args)
    {
        EventSender es = new EventSender();

        Subscriber s = new Subscriber();
        es.MyEvent += s.OnEvent;

        s = new Subscriber();
        es.MyEvent += s.OnEvent;

        es.MyEvent();

        Console.ReadKey();
    }
}

在这里,“OnEvent”将被打印两次。即使看起来我已经释放了它的句柄,也会保留对订阅的引用。这是由于代表如何保留其订阅者列表。

如果这是问题所在,您需要每次取消订阅:

es.MyEvent -= s.OnEvent

这应该在您失去对订阅者的句柄之前完成(即 befores超出范围或null)。您可以考虑在订阅者中跟踪您的事件源,并有一个Dispose为您取消订阅的方法。

此外,正如其他人所指出的,您可以在订阅之前取消订阅 :) 我确信现在您已经拥有所需的解决方案。

于 2014-08-07T19:18:30.827 回答
0

我有同样的问题,在同步方法中注册你的事件(我把它放在 form_loaded 中)

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        RefreshHierarchy.COIDConflict += RefreshHierarchy_COIDConflict;
    }
于 2018-03-27T12:57:14.457 回答