zoukankan      html  css  js  c++  java
  • AOP之PostSharp6EventInterceptionAspect(事件异步调用)

        在上几章我们讨论了方法属性字段的aspect,现在我们再来看看事件机制的aspect。和字段,属性location一样,在c#中字段也可以转化为方法名为add,remove的方法处理,所以对于事件的aspect,同样类似于我们的方法。我们先看看EventInterceptionAspect的定义:

    image

    aspect类包含我们对于事件aspect所必要的注册,取消,调用的注入。

    其参数定义如下:

    image

    为我们提供了,ProceedAddHandler,ProceedInvokeHandler,ProceedRemoveHandler的事件处理代理。同样包含来自AdviceArgs的Instance对象。

      对于事件aspect的例子真的不好想,在这里我们只是简单的做个事件变为异步调用的代码作为demo:

    [Serializable]
        public class AsynEventAspectAttribute : PostSharp.Aspects.EventInterceptionAspect
        {

            public override void OnInvokeHandler(PostSharp.Aspects.EventInterceptionArgs args)
            {
                var th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(new Action<object>((obj) =>
                    {
                        System.Threading.Thread.Sleep(new Random().Next(1000));
                        try
                        {
                            args.ProceedInvokeHandler();
                        }
                        catch (Exception ex)
                        {

                            args.ProceedRemoveHandler();
                        }
                    })));
                th.Start();
            }
        }

     测试代码:

    namespace PostSharpDemo 

        public class TestAsyncAspect 
        { 
            [AsynEventAspectAttribute] 
            public event EventHandler SomeEvent = null

            public void OnSomeEvent() 
            { 
                if (SomeEvent != null
                { 

                    SomeEvent(this, EventArgs.Empty); 
                } 
            } 
        } 
    }
    class Program 
        { 
            static void Main(string[] args) 
            {

    TestAsyncAspect pro = new TestAsyncAspect(); 
              for (int i = 0; i < 10; i++) 
              { 
                  pro.SomeEvent += new EventHandler(pro_SomeEvent); 
              } 
              pro.OnSomeEvent(); 
        //      pro.SomeEvent -= new EventHandler(pro_SomeEvent); 
              Console.WriteLine("主线程完了!"); 
              Console.Read(); 
          } 

          static void pro_SomeEvent(object sender, EventArgs e) 
          { 
              Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId); 
          }    

    }  

     效果图如下:

    image

    附件下载:demo

  • AOP之PostSharp初见-OnExceptionAspect
  • AOP之PostSharp2-OnMethodBoundaryAspect
  • AOP之PostSharp3-MethodInterceptionAspect
  • AOP之PostSharp4-实现类INotifyPropertyChanged植入
  • AOP之PostSharp5-LocationInterceptionAspect
  • AOP之PostSharp6-EventInterceptionAspect
  • http://www.cnblogs.com/whitewolf/category/312638.html
查看全文
  • 相关阅读:
    MySql存储过程—2、第一个MySql存储过程的建立
    MYSQL 存储过程1、SQL存储过程的基础知识
    [转]忍无可忍 献上一曲维族歌曲 《我们的客户是花园》(吐槽)
    由硬盘供电不稳、数据线品质差造成的蓝屏
    music Elton John
    一个人在艰苦中能雄起,那是能力和毅力,一个人在安逸中能雄起,那是自觉和自律。
    安装VS2012出问题后,反复重启电脑。
    山寨、低端、劣质的机箱前置面板的悲剧。
    SQL Server Management Studio (SSMS) 清除登录记录
    DataTable 分批处理,每批处理4行
  • 原文地址:https://www.cnblogs.com/whitewolf/p/PostSharp6.html
  • Copyright © 2011-2022 走看看