利用委托实现异步调用
2016-03-29 23:52
309 查看
同步调用示例(委托是一个类型安全的,面向对象的指针)
剖析代码的继承关系: 代理Operate经历了3次派生(绿色部分),实现了一个构造和3个虚拟函数(黄色部分).
异步调用方法:线程异步了,但是主线程等待次线程返回才能获得结果,阻塞在EndInvoke上.
同步调用:如果线程没有返回,不要一直问何时返回,每间隔500ms做该做的事情!!!
线程回调: 好吧,干脆你返回的时候通知我,我该干啥不耽搁!
主线程向次线程传递对象,通知!
Main() invoked on thread:1
Main() execute no need to wait any more.
Add() invoked on thread:3
AddComplete() invoked on thread:3
The value is 30.
The Main() thread is :1
推荐下博客客户端工具: Download
using System; using System.Threading; namespace Demo { public delegate int Operate(int x, int y); public class DelegateAsync { static int Add(int a, int b) { Console.WriteLine ("Add() invoked on thread:{0}\n", Thread.CurrentThread.GetHashCode ()); Thread.Sleep (5000); return a + b; } static void Main() { Console.WriteLine ("Main() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); //Add() call in Synchronouse mode Operate op = new Operate (Add); int answer = op (10, 20); //after Add() executed, the folow goes on. Console.Write("After Add() executed, result = {0}",answer); Console.ReadKey (); } } }
剖析代码的继承关系: 代理Operate经历了3次派生(绿色部分),实现了一个构造和3个虚拟函数(黄色部分).
异步调用方法:线程异步了,但是主线程等待次线程返回才能获得结果,阻塞在EndInvoke上.
using System; using System.Threading; namespace Demo { public delegate int Operate(int x, int y); public class DelegateAsync { static int Add(int a, int b) { Console.WriteLine ("Add() invoked on thread:{0}\n", Thread.CurrentThread.GetHashCode ()); Thread.Sleep (5000); return a + b; } static void Main() { Console.WriteLine ("Main() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); //Add() call in Synchronouse mode Operate op = new Operate (Add); IAsyncResult result = op.BeginInvoke(10,20,null,null); Console.WriteLine ("Doing more in Main() exected immediately."); //the thread susppend on EndInvoke while 'result' is ready! int answer = op.EndInvoke (result); Console.Write("After Add() executed, result = {0}",answer); Console.ReadKey (); } } }
同步调用:如果线程没有返回,不要一直问何时返回,每间隔500ms做该做的事情!!!
using System; using System.Threading; namespace Demo { public delegate int Operate(int x, int y); public class DelegateAsync { static int Add(int a, int b) { Console.WriteLine ("Add() invoked on thread:{0}\n", Thread.CurrentThread.GetHashCode ()); Thread.Sleep (5000); return a + b; } static void Main() { Console.WriteLine ("Main() invoked on thread:{0}\n", Thread.CurrentThread.GetHashCode ()); //Add() call in Synchronouse mode Operate op = new Operate (Add); IAsyncResult result = op.BeginInvoke(10,20,null,null); //while result is not OK, do it every 500 ms. while (!result.AsyncWaitHandle.WaitOne(500,true)) {//result.IsCompleted Console.WriteLine ("Doing more work in Main()."); } int answer = op.EndInvoke (result); Console.Write("After Add() executed, result = {0}",answer); Console.ReadKey (); } } }
线程回调: 好吧,干脆你返回的时候通知我,我该干啥不耽搁!
using System; using System.Threading; /// <summary> /* Main() invoked on thread:1 Main() execute no need to wait any more. Add() invoked on thread:3 AddComplete() invoked on thread:3 Your Addition is complete. */ /// </summary> namespace Demo { public delegate int Operate(int x, int y); public class DelegateAsync { static int Add(int a, int b) { Console.WriteLine ("Add() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); Thread.Sleep (5000); return a + b; } static void AddComplete(IAsyncResult ia) { Console.WriteLine ("AddComplete() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); //Get Result value System.Runtime.Remoting.Messaging.AsyncResult ar= (System.Runtime.Remoting.Messaging.AsyncResult)ia; Operate op = (Operate)ar.AsyncDelegate; Console.WriteLine ("The value is {0}.", op.EndInvoke (ia)); } static void Main() { Console.WriteLine ("Main() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); //Add() call in Synchronouse mode Operate op = new Operate (Add); IAsyncResult result = op.BeginInvoke (10, 20, new AsyncCallback (AddComplete), null); Console.WriteLine ("Main() execute no need to wait any more."); Console.ReadKey (); } } }
主线程向次线程传递对象,通知!
using System; using System.Threading; /// <summary> /*
Main() invoked on thread:1
Main() execute no need to wait any more.
Add() invoked on thread:3
AddComplete() invoked on thread:3
The value is 30.
The Main() thread is :1
*/ /// </summary> namespace Demo { public delegate int Operate(int x, int y); public class DelegateAsync { static int Add(int a, int b) { Console.WriteLine ("Add() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); Thread.Sleep (5000); return a + b; } static void AddComplete(IAsyncResult ia) { Console.WriteLine ("AddComplete() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); //Get Result value System.Runtime.Remoting.Messaging.AsyncResult ar= (System.Runtime.Remoting.Messaging.AsyncResult)ia; Operate op = (Operate)ar.AsyncDelegate; Console.WriteLine ("The value is {0}.", op.EndInvoke (ia)); Thread thread = (Thread)ia.AsyncState; Console.WriteLine ("The Main() thread is :{0}", thread.GetHashCode()); } static void Main() { Console.WriteLine ("Main() invoked on thread:{0}", Thread.CurrentThread.GetHashCode ()); //Add() call in Synchronouse mode Operate op = new Operate (Add); IAsyncResult result = op.BeginInvoke (10, 20, new AsyncCallback (AddComplete),Thread.CurrentThread); Console.WriteLine ("Main() execute no need to wait any more."); Console.ReadKey (); } } }
推荐下博客客户端工具: Download
相关文章推荐
- gdal读写图像分块处理(精华版)
- IOS中CocoaPods 从安装到使用全解
- getResourceAsStream 讲解
- 项目中乱码问题处理总结
- Page 63-64 Exercises 2.3.7 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)
- 小读Thrift的PHP源码
- 统计学中常用专用名词中英对照
- 正确配置Linux系统ulimit值的方法
- Piktochart(制作信息图表)
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- myHomework Student Planner(学习规划)
- android ptl
- 主从 mysql (master slave) 复制原理及配置
- 20160228.CCPP体系详解(0038天)
- myHomework Student Planner(学习规划)
- 2016第13周二
- 找不到或无法加载主类
- 内存泄露之Context泄露
- 2015年蓝桥杯C组第二题:立方尾不变
- java面向对象特征