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

  • 相关阅读:
    hdu--4336--概率dp
    hdu--3905--dp
    codeforces--279--
    hdu--5023--线段树
    正则表达式
    vim编辑器使用
    圆头像控件,自动监听点击跳转到Activity
    ImageView切换两种状态下的模式
    string字符串截取
    Class对象获取方法
  • 原文地址:https://www.cnblogs.com/cplover/p/4527112.html
Copyright © 2011-2022 走看看