zoukankan      html  css  js  c++  java
  • c# 中 EventHandler<TEventArgs>的 异步调用

    在实现CSharpServer框架时,需要写一个EventDispatcherUtil用于触发某个事件。
    使用c# 内置的EventHandler直接调用handler.BeginInvoke可以异步调用EventHandler里的调用链的监听的事件方法
    但如果是自定义的EventHandler<TEventArgs> 类型调用BeginInvoke会产生异常(该委托必须有一个目标),所以需要自行实现EventHandler的异步调用
    下面有一种方案是获取EventHandler<TEventArgs>的所有Delegate[],再调用每个Delegate的BeginInvoke(Delegate类型没有BeginInvoke方法,需要转换成EventHandler<TEventArgs>):

    public static void AsyncDispatcherEvent<TEventArgs>(EventHandler<TEventArgs> Handler, object Sender, TEventArgs Args, AsyncCallback EndInvokeCallback = null) where TEventArgs : EventArgs
    {
      if (Handler == null)
      {
        return;
      }
      if (EndInvokeCallback == null)
      {
        EndInvokeCallback = DefaultEndCallbackArgs<TEventArgs>;
      }
      var deles = Handler.GetInvocationList();
      foreach (EventHandler<TEventArgs> handler in deles)
      {
        handler.BeginInvoke(Sender, Args, EndInvokeCallback, handler);
      }
    }

  • 相关阅读:
    Hibernate 与 mybatis 区别
    Struts2 核心流程
    java 面试 -- 4
    无线电日记 2
    ham 无线电笔记
    Our Deepest Fear
    随笔
    GSM学习笔记
    网络时代的悲哀:微软百科全书
    [转载]arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
  • 原文地址:https://www.cnblogs.com/cplover/p/4527112.html
Copyright © 2011-2022 走看看