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

  • 相关阅读:
    Numpy存字符串
    一个类似于postman的协议测试工具
    freetds设置超时
    学习jQuery
    webpy 使用python3开发
    gdb调试coredump文件
    htop和ncdu
    rqalpha-自动量化交易系统(一)
    perl学习-运算符添加引号
    xss 和 csrf攻击详解
  • 原文地址:https://www.cnblogs.com/cplover/p/4527112.html
Copyright © 2011-2022 走看看