• <bdo id='2M7NR'></bdo><ul id='2M7NR'></ul>
    <tfoot id='2M7NR'></tfoot>

      <i id='2M7NR'><tr id='2M7NR'><dt id='2M7NR'><q id='2M7NR'><span id='2M7NR'><b id='2M7NR'><form id='2M7NR'><ins id='2M7NR'></ins><ul id='2M7NR'></ul><sub id='2M7NR'></sub></form><legend id='2M7NR'></legend><bdo id='2M7NR'><pre id='2M7NR'><center id='2M7NR'></center></pre></bdo></b><th id='2M7NR'></th></span></q></dt></tr></i><div id='2M7NR'><tfoot id='2M7NR'></tfoot><dl id='2M7NR'><fieldset id='2M7NR'></fieldset></dl></div>

      <small id='2M7NR'></small><noframes id='2M7NR'>

    1. <legend id='2M7NR'><style id='2M7NR'><dir id='2M7NR'><q id='2M7NR'></q></dir></style></legend>
    2. c#事件处理程序在一次引发事件时被多次调用

      c# event handler is called multiple times when event is raised once(c#事件处理程序在一次引发事件时被多次调用)
      <i id='KzcOj'><tr id='KzcOj'><dt id='KzcOj'><q id='KzcOj'><span id='KzcOj'><b id='KzcOj'><form id='KzcOj'><ins id='KzcOj'></ins><ul id='KzcOj'></ul><sub id='KzcOj'></sub></form><legend id='KzcOj'></legend><bdo id='KzcOj'><pre id='KzcOj'><center id='KzcOj'></center></pre></bdo></b><th id='KzcOj'></th></span></q></dt></tr></i><div id='KzcOj'><tfoot id='KzcOj'></tfoot><dl id='KzcOj'><fieldset id='KzcOj'></fieldset></dl></div>
        <bdo id='KzcOj'></bdo><ul id='KzcOj'></ul>

          <tbody id='KzcOj'></tbody>

          <small id='KzcOj'></small><noframes id='KzcOj'>

        1. <tfoot id='KzcOj'></tfoot>

            <legend id='KzcOj'><style id='KzcOj'><dir id='KzcOj'><q id='KzcOj'></q></dir></style></legend>
              • 本文介绍了c#事件处理程序在一次引发事件时被多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                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 even though the event is raised once, the event is firing 20+ times on the class that consumes this event. Any ideas?

                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 事件的类:

                Class that uses IBSerialPort and consumes its OnPacketReceived Event:

                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!!!!");
                }
                

                推荐答案

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

                Try this, this will unregister any prev subscriber:

                ibSerialPort.OnPacketReceived -= ibSerialPort_OnPacketReceived;   // unregister
                ibSerialPort.OnPacketReceived += ibSerialPort_OnPacketReceived;  //register
                

                这篇关于c#事件处理程序在一次引发事件时被多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
              • <small id='RwUYK'></small><noframes id='RwUYK'>

                  <bdo id='RwUYK'></bdo><ul id='RwUYK'></ul>

                    <i id='RwUYK'><tr id='RwUYK'><dt id='RwUYK'><q id='RwUYK'><span id='RwUYK'><b id='RwUYK'><form id='RwUYK'><ins id='RwUYK'></ins><ul id='RwUYK'></ul><sub id='RwUYK'></sub></form><legend id='RwUYK'></legend><bdo id='RwUYK'><pre id='RwUYK'><center id='RwUYK'></center></pre></bdo></b><th id='RwUYK'></th></span></q></dt></tr></i><div id='RwUYK'><tfoot id='RwUYK'></tfoot><dl id='RwUYK'><fieldset id='RwUYK'></fieldset></dl></div>

                            <tbody id='RwUYK'></tbody>
                          <legend id='RwUYK'><style id='RwUYK'><dir id='RwUYK'><q id='RwUYK'></q></dir></style></legend>

                          <tfoot id='RwUYK'></tfoot>