AOP之PostSharp6-EventInterceptionAspect(事件异步调用)
2011-12-13 19:50
417 查看
在上几章我们讨论了方法属性字段的aspect,现在我们再来看看事件机制的aspect。和字段,属性location一样,在c#中字段也可以转化为方法名为add,remove的方法处理,所以对于事件的aspect,同样类似于我们的方法。我们先看看EventInterceptionAspect的定义:
aspect类包含我们对于事件aspect所必要的注册,取消,调用的注入。
其参数定义如下:
为我们提供了,ProceedAddHandler,ProceedInvokeHandler,ProceedRemoveHandler的事件处理代理。同样包含来自AdviceArgs的Instance对象。
对于事件aspect的例子真的不好想,在这里我们只是简单的做个事件变为异步调用的代码作为demo:
测试代码:
效果图如下:
附件下载: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
aspect类包含我们对于事件aspect所必要的注册,取消,调用的注入。
其参数定义如下:
为我们提供了,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); } }
效果图如下:
附件下载: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
相关文章推荐
- AOP之PostSharp6-EventInterceptionAspect(事件异步调用)
- AOP之PostSharp5-LocationInterceptionAspect
- AOP之PostSharp5-LocationInterceptionAspect
- CodeDom系列--事件(event)定义和反射调用
- EventBus源码分析(三):post方法发布事件【获取事件的所有订阅者,反射调用订阅者事件处理方法】(2.4版本)
- C#中的异步调用及异步设计模式(三)——基于事件的异步模式
- iphone 消息处理事件 postNotificationName和addObserver的调用
- 关于Android事件分发中onInterceptTouchEvent调用时机
- RestTemplate之GET和POST调用和异步回调
- CodeDom系列--事件(event)定义和反射调用
- flex中遇到监听事件异步调用
- jQuery异步调用post,get (重要)
- 应用程序调用_驱动B调用_驱动A_异步方法1_带完成历程_事件
- 在Event内部监测鼠标事件,对窗体内控件调用move动作,UI不刷新
- 委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件【转】
- 远端异步调用事件结果
- Net事件的异步调用模式
- QT多线程及通过事件进行通信(通过自定义事件,然后QApplication::postEvent给主界面,我之前用的是信号槽)
- C# 委托事件的异步调用
- 委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件