c# event handler is called multiple times when event is raised once

前端 未结 4 1661
-上瘾入骨i
-上瘾入骨i 2021-01-05 04:42

Below is my code, first is where I raise the event and second section is where I consume it in another class. It seems pretty straight forward, but the logs are showing that

4条回答
  •  梦毁少年i
    2021-01-05 05:16

    How many times is this being called? If this gets called multiple times then your event will be called multiple times.

     ibSerialPort.OnPacketReceived += ibSerialPort_OnPacketReceived;
    

    As a test, you could remove the delegate just before you add it:

    ibSerialPort.OnPacketReceived -= ibSerialPort_OnPacketReceived;
    ibSerialPort.OnPacketReceived += ibSerialPort_OnPacketReceived;
    

提交回复
热议问题